Browse Source

Fixed formatting and made it take file as argument

master
Macoy Madson 6 months ago
parent
commit
56d38198f0
3 changed files with 22 additions and 17 deletions
  1. +1
    -1
      ReadMe.org
  2. +3
    -5
      TestPrint.org
  3. +18
    -11
      ThermalPrinterConverter.py

+ 1
- 1
ReadMe.org View File

@@ -13,6 +13,6 @@ sudo apt install -y system-config-printer
[[https://www.techrepublic.com/article/control-printers-in-linux-from-the-command-line/][Command line operation]]
* Use
#+BEGIN_SRC sh
python3 EpsonESCPOSConverter.py
python3 TermalPrinterConverter.py
./printOutput.sh
#+END_SRC

+ 3
- 5
TestPrint.org View File

@@ -1,9 +1,7 @@
#+TITLE:Big Title with some text that needs wrapping
#+TITLE:Big Title with wrapping
* Heading
Welcome to the test print.
** Subheading 1
This is some text which is going to cause the printer to wrap the line. Is it going to wrap the line properly? Only time will tell.
This is some text which is going to cause the printer to wrap the line.
*** This is a deeper heading
Any heading as deep or deeper will look the same as this one.
** Subheading 2
Here's another heading.
It's deep

+ 18
- 11
ThermalPrinterConverter.py View File

@@ -1,3 +1,4 @@
import sys
import textwrap

# Output a binary file which a ESCPOS thermal printer will understand
@@ -29,6 +30,8 @@ def writeOutputBuffer():
# I think the printer needs to be told this is the desired format
# encodedString = gOutputBuffer.encode("gb18030")
print("Outputting to {}".format(outputFilename))
print("Use the following command (replacing [your printer]) to print:\n"
"lpr -P [your printer] output.bin")
outFile = open(outputFilename, "wb")
outFile.write(encodedString)
outFile.close()
@@ -59,28 +62,28 @@ def outputTextBlock(outString):
wrapWidth = lineWrapColumns[gCurrentTextStyle]
# Fix newlines and wrap
# lines = outString.replace('\n', newLine).split(newLine)
# print("'" + outString + "'")
# We will be adding this newline later, and it will just confuse the splitter.
if outString[-1] == '\n':
outString = outString[:-1]
lines = outString.split("\n")
wrappedLines = []
for line in lines:
if not line:
wrappedLines.append('')
wrappedLines += textwrap.wrap(line, width=wrapWidth)
# print(wrappedLines)
# exit()
for line in wrappedLines:
print('"' + line + '"')
if line:
gOutputBuffer += line + newLine
# print(line)
else:
gOutputBuffer += newLine
# print("")

def lineHasTagExactly(line, tag):
return len(line) >= len(tag) and tag in line[:len(tag)]

def lineGetTaggedValue(line, tag):
return line[len(tag):]
def orgModeToEscPos(orgLines):
for line in orgLines:
if lineHasTagExactly(line, '#+TITLE:'):
@@ -94,18 +97,18 @@ def orgModeToEscPos(orgLines):
setTextStyle(TextStyle_RegularEmphasis)
outputTextBlock("\n" + lineGetTaggedValue(line, '** '))
# All deeper headings are just bolded and have a space at the start
elif line[0] == "*":
elif len(line) > 2 and line[0] == "*" and line[1] == "*":
setTextStyle(TextStyle_RegularEmphasis)
outputTextBlock("\n" + line[line.find(" "):])
# Body text
else:
setTextStyle(TextStyle_Regular)
outputTextBlock(line, TextStyle_Regular)
outputTextBlock(line)

def main():
def main(filenameToConvert):
outputInitializationCode()

orgFile = open("TestPrint.org", "r")
orgFile = open(filenameToConvert, "r")
orgLines = orgFile.readlines()
orgFile.close()

@@ -163,4 +166,8 @@ def main():
# outFile.close()

if __name__ == "__main__":
main()
if len(sys.argv) != 2:
print("Org Mode to ESCPOS\nUsage:\n\tpython3 ThermalPrinterConverter.py MyDoc.org")
sys.exit(1)
else:
main(sys.argv[1])

Loading…
Cancel
Save