Print to standard printer from Python?
Solution 1
Unfortunately, there is no standard way to print using Python on all platforms. So you'll need to write your own wrapper function to print.
You need to detect the OS your program is running on, then:
For Linux -
import subprocess
lpr = subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE)
lpr.stdin.write(your_data_here)
For Windows: http://timgolden.me.uk/python/win32_how_do_i/print.html
More resources:
Print PDF document with python's win32print module?
How do I print to the OS's default printer in Python 3 (cross platform)?
Solution 2
To print to any printer on the network you can send a PJL/PCL print job directly to a network printer on port 9100.
Please have a look at the below link that should give a good start:
http://frank.zinepal.com/printing-directly-to-a-network-printer
Also, If there is a way to call Windows cmd you can use FTP put to print your page on 9100. Below link should give you details, I have used this method for HP printers but I believe it will work for other printers.
http://h20000.www2.hp.com/bizsupport/TechSupport/Document.jsp?objectID=bpj06165
Solution 3
You can try wx library. It's a cross platform UI library. Here you can find the printing tutorial: https://web.archive.org/web/20160619163747/http://wiki.wxpython.org/Printing
Solution 4
I find this to be the superior solution, at least when dealing with web applications. The idea is this: convert the HTML page to a PDF document and send that to a printer via gsprint
.
Even though gsprint
is no longer in development, it works really, really well. You can choose the printer and the page orientation and size among several other options.
I convert the web page to PDF using Puppeteer, Chrome's headless browser. But you need to pass in the session cookie to maintain credentials.
Prof. Falken
Updated on May 12, 2020Comments
-
Prof. Falken about 4 years
Is there a reasonably standard and cross platform way to print text (or even PS/PDF) to the system defined printer?
Assuming CPython here, not something clever like using Jython and the Java printing API.
-
Prof. Falken over 9 yearsThis is very promising! I don't work with python + printing any more, but if printing demonstrably works cross platform with wxpython, I would accept this answer.
-
TheTechRobo Stands for Ukraine about 4 yearsI would love this, but it isn't cross-platform :(
-
TheTechRobo Stands for Ukraine about 4 yearsThanks for your answer! I used your answer in conjunction with @user3444876 's answer.
-
Bobort about 4 yearsWell, you could use the CUPS system in Linux. unix.stackexchange.com/questions/421341/…
-
Shmack almost 4 yearsIf I remember correctly, if you want to use this commercially, you'll need a commercial license, which is not free. Not a problem I suppose if you are a big business, but might be annoying for a small company, like mine.
-
Bobort almost 4 years@ShanerM13, I just looked this up. It uses the Aladdin Free Public License, which means it's basically free for small businesses that are using it in-house.