48

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
  • 24,226
  • 19
  • 100
  • 173
  • Assuming Java printing can do this another "clever" solution is JPype(a way to get cpython and the jvm to communicate/run code on each other) – Roman A. Taycher Oct 04 '12 at 10:47

5 Answers5

58

This only works on Windows.

You can do the following:

import os

os.startfile("C:/Users/TestFile.txt", "print")

This will start the file, in its default opener, with the verb 'print', which will print to your default printer.Only requires the os module which comes with the standard library

  • 1
    according to https://docs.python.org/2/library/os.html#os.startfile this only works on windows – Buzz Jan 03 '17 at 21:43
  • Do you know of any way to customize the printing to print in landscape mode? – Alex F May 29 '19 at 18:00
  • Thank you for this answer! I used your answer in conjunction with @AnujGupta 's answer. – TheTechRobo the Nerd May 12 '20 at 12:00
  • 1
    The Linux or Mac option is using the same OS module: import os os.system("lpr -P printer_name file_name.txt") Where "printer_name" represents the name of the printer and "file_name.txt" is the name of the file that will be printed. – xPTM1219 Apr 01 '21 at 01:04
  • This works fine for text files, but does not work with images. How can we print a jpg image with this method? – knowledge_seeker Jun 28 '21 at 18:36
37

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)?

Community
  • 1
  • 1
Anuj Gupta
  • 10,056
  • 3
  • 28
  • 32
9

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

glglgl
  • 89,107
  • 13
  • 149
  • 217
4

You can try the wxPython 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

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
Admdebian
  • 630
  • 6
  • 17
  • This 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. – Prof. Falken Mar 06 '15 at 08:17
  • I apologize @Admdebian, I know you posted this 7 years ago but today I accidentally downvoted your answer (thick fingers). If you make an edit, I'll remove my downvote. – nordmanden Aug 28 '22 at 16:39
0

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.

Bobort
  • 3,085
  • 32
  • 43
  • I would love this, but it isn't cross-platform :( – TheTechRobo the Nerd May 11 '20 at 21:14
  • 1
    Well, you could use the CUPS system in Linux. https://unix.stackexchange.com/questions/421341/print-pdf-from-command-line-in-linux-similar-to-enscript-2lr – Bobort May 13 '20 at 02:13
  • If 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. – Shmack Jul 13 '20 at 15:32
  • @ShanerM13, I just looked this up. It uses the [Aladdin Free Public License](https://en.wikipedia.org/wiki/Aladdin_Free_Public_License), which means it's basically free for small businesses that are using it in-house. – Bobort Jul 13 '20 at 21:12