Using a Thermal Printer


Let me walk you through how to use a serial receipt printer with Python 3.  To the right is an video demo I made where I recreated the text scroll from Star Wars: A New Hope.  This skill will allow you to have a physical output from your scripts.  There are all kinds of uses for this type of skill.

Okay first we need to import the PySerial library.

To install it from command line run the following:

pip install pyserial

import serial

Starting the Connection

Now we need to set the variables for the script.  You can easily change these values to what you need.

‘com’ is for the serial com port you are using.
‘baud’ is the baud rate of your printer.
‘width’ is the character width of your printer.
‘cutfeed’ is how many lines to feed before cutting.

#Set Variables
com = 'COM1'
baud = 38400
width = 42
cutfeed = 6

This starts the printer communications.  Remember as you go you will need to run ‘ser.close()’ to end the connection.  If you don’t close it and the script crashes you may have issues restarting the script because the com port is left open.

ser = serial.Serial(com, baud, timeout=0,
if ser.isOpen():

Closing the Connection

This makes closing the connection easier as you go.  If you try closing the connection while it is already closed it will cause issues.  This method will check if it is open before closing it.

#closes the serial connection
#you must end any print process with this command.
def close():
    if ser.isOpen():

Cutting the Paper

This is the cut paper command.  It is dependent on a command (linefeed) we haven’t added yet.  Basically it will feed a count of lines and the cut the paper.  The line ‘ser.write(b”\x1D\x56\x00″)’ is the serial command to trigger the cutter.

#cuts the paper
def cutpaper():
    if not ser.isOpen():
    line = range(cutfeed)
    for i in line:

Cut and Close

This command is a shortcut to cut paper and end the connection.

#shortcut to cutpaper and close the connection
def end():

Resets ESC/POS Defaults

The ‘init’ command resets different settings we can send to the printer.  Later we can change the text size, direction, etc.  This resets it back to the defaults.

#resets all commands sent to printer
def init():
     if not ser.isOpen():


Force a Line Feed

This will feed a single line.  This is a dependency for the ‘cutpaper’ command.

#Feeds a single line.
def linefeed():
    if not ser.isOpen():

Printing a String

Finally we get to a print command.  This takes an argument (a string) and prints it on the paper and then it runs the ‘linefeed’ command.  The text will be default justified.

#prints a string
def prt(item):
    if not ser.isOpen():

Printing a Centered Single String

The ‘prtcent’ command takes a string and centers it before printing it.  For this to work properly it is important that your ‘width’ variable is correct.  If you have many strings to print centered I would use ‘setjust(2)’ and ‘prt()’.

#prints a centered string
def prtcent(item):
    if not ser.isOpen():

Printing a List of Strings

This command will take a list object.  It goes through each string in the list and prints it.  Use the ‘setjust(2)’ command to make it centered.  I really like this one and it makes things a lot easier when doing a large body of text.

#prints a list.
def prtlst(item):
    for i in item:

Rotating the Text

This command will rotate the text clockwise.  It takes an argument.  Using ‘1’ will rotate the text and using ‘0’ will set it back to normal.

#rotates characters clockwise
def rotatecw(item):
    if not ser.isOpen():
    if item == 0:#cancels
    elif item == 1:#rotates


This command can justify the text for use with the ‘prt’ command.
‘0’ will left justify
‘1’ will right justify
‘2’ will center justify

#sets justification
def setjust(item):
    if not ser.isOpen():
    if item == 0:#left
    elif item == 1:#right
    elif item == 2:#center
        print('Invalid Entry') 

Changing the Text Size

This will change the size of the text.  This comes in real handy.
‘0’ is normal size
‘1’ is double size
‘2’ is triple size

The key to it is in the last set in the command sent:


Changing the ’00’ to ’11’ will double the size.  You can make the characters wider or taller by changing those individually.  The first character is the width and the second is the height.

def setsize(item):
    if not ser.isOpen():
    if item == 0:#Size Normal
    elif item == 1:#Size Double
    elif item == 2:#Size Triple

Printing Upside Down

This will make your text upside down.  Passing a ‘1’ will make roll it upside down and ‘0’ will make it normal again.  This can come in handy if your printer is mounted on the wall or something.

#print text upside down
def upsidedown(item):
    if not ser.isOpen():
    if item == 0:#right side up
    elif item == 1:#upside down

As I am working on this project I will add more items to this page.  If you have any feedback or if you know of any commands I can add please send them my way to  Thank you.