In this tutorial, we’ll explore how to effortlessly print receipts, invoices, and tickets on a thermal printer using Python. I’ll introduce you to a powerful plugin that I’ve developed, enabling seamless integration for Python-based applications.
With this plugin, you’ll gain the ability to effortlessly print tickets from Python, regardless of your preferred framework—whether it’s the terminal, Flask, Tkinter, Django, or any other Python-supported platform.
It provides extensive functionality, allowing you to print images, barcodes, text with accents, change fonts, rotate text, and much more, sending the ESC POS commands native to thermal printers.
Join me as we dive into the world of thermal printing using Python, empowering you to generate professional-quality prints effortlessly.
To make this work, you will need the plugin running in the background and your printer shared as explained in:
I encourage you to read it first and print the “Hello ESC POS printer” receipt even if it is in another programming language.
Python ESC POS connector
To print on a thermal printer from Python, we need a connector that acts as a link between Python and the plugin. I have programmed it myself, and it looks like this:
Note: You will need the requests package, which you can easily install with
pip install requests.
What I showed you earlier is the connector. Your Python code uses the connector, and the connector communicates with the plugin to print on thermal printers. That’s it.
Hello Thermal printer from Python
So, in our file where we have our Python code, we are going to import the connector like this:
The connector should be in a file called
ConectorPython.py. However, if you prefer not to import it, you can also copy and paste the entire code into your project. Just be careful not to get confused.
Now, you can obtain the list of printers like this:
That will return the list of printers. It is a list of strings and will be useful if you want to give the user the opportunity to choose their printer or perform similar tasks.
Now, let’s create an instance of
ConectorV3 and print a “Hello, World” message:
Please note that your printer’s name must be specified in
Note: If the plugin is listening on a different port, remember that you can specify the URL when constructing the
ConectorV3 object or when invoking
Python Example: Printing a Ticket on a Thermal Printer using ESC POS
Discover a complete example of printing a ticket on a thermal printer using Python and ESC POS.
In this sample code, the printer name is provided directly, assuming you have prior knowledge of the desired printer. However, you can modify the code to allow users to choose from a printer list for more flexibility.
It could be a little confusing at the beggining but once you know every function, you will be able to unlash your creativity and print any type of receipts.
When running that code, the output should be like this:
As you can see, we are printing images on a thermal printer using Python, along with QR codes, barcodes, styled text, and custom characters.
Now, what remains is to explore the documentation on the plugin’s webpage to familiarize yourself with all the available methods and the full extent of its capabilities.
You can find the documentation on the plugin’s webpage: https://parzibyte.me/blog/en/2019/10/13/print-ticket-in-thermal-printer-directly-from-browser/