Search code examples
pythonprintingcpython

Print to standard printer from Python?


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.


Solution

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