Skip to content

Instantly share code, notes, and snippets.

@frederickding
Created October 16, 2022 16:54
Show Gist options
  • Save frederickding/82e527bb3890c2b7299fceeaeff4782a to your computer and use it in GitHub Desktop.
Save frederickding/82e527bb3890c2b7299fceeaeff4782a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from escpos import *
import argparse
import os
import sys
import datetime
cli_parser = argparse.ArgumentParser(description='Print to a network ESCPOS receipt printer.')
cli_parser.add_argument('file', help='text file to print')
cli_parser.add_argument('--printer', '-p', default="192.168.1.253", help='IP address or hostname of printer')
cli_parser.add_argument('--nodate', action='store_true', help='Do not print date')
cli_parser.add_argument('--nocut', action='store_true', help='Do not cut at end')
cli_parser.add_argument('--header', action='store_true', help='Print filename as header')
cli_args = cli_parser.parse_args()
def main():
p = printer.Network(cli_args.printer)
inputtext = []
if not os.path.exists(cli_args.file):
print("File %s does not exist" % cli_args.file, file=sys.stderr)
exit(1)
with open(cli_args.file) as textfile:
inputtext = textfile.readlines()
if len(inputtext) < 1:
exit(2)
if cli_args.header:
p.control("CR")
p.control("LF")
p.set("CENTER", "A", "B")
p.text(os.path.basename(cli_args.file))
p.text(" ")
p.control("CR")
p.control("LF")
p.set("LEFT", "B", "normal")
p.control("CR")
p.control("LF")
p.set("LEFT")
for line in inputtext:
if line:
p.text(line)
p.control("CR")
p.control("LF")
if not cli_args.nodate:
now = datetime.datetime.now()
p.set("CENTER")
p.text(now.strftime("%Y-%m-%d %H:%M:%S"))
p.control("CR")
p.control("LF")
if not cli_args.nocut:
p.cut()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment