Print to standard printer from Python?
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)?
How to send print job to printer in python
try this! It uses os module to start the file in the default printer!
import os
os.startfile("YourDocument", "print")
Is There a way to print pictures With a printer using python?
This will only work on windows (10 or 11):
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
setting default printer custom page size with python
ShellExecute
is using the default printing parameters. If you need to use the reset DevMode
for printing, you can use CreateDC
.
Refer: GDI Print API
If you use SetPrinter to modify the default DEVMODE structure for a
printer (globally setting the printer defaults), you must first call
the DocumentProperties function to validate the DEVMODE structure.
Refer:
- SetPrinter Remarks
- Modify printer settings by using the SetPrinter function
You can also directly use DocumentProperties
to modify printer initialization information.
Then pass pDevModeObj
to CreateDC
, and use StartDoc
and StartPage
to print.
Similar case: Change printer tray with pywin32
Printing a file and configure printer settings
Look at the "How To Print" page on Tim Golden's website. This page was the same in 2014 when you asked your question. There's an example printing a JPG file that also manipulates the printer settings.
That's not quite a perfect example for what you're doing but it should get you on the right track.
DC_PAPERS is defined in win32con:
import win32con
x = win32con.DC_PAPERS
How you're supposed to know that, I have no idea. Perhaps it's "obvious" to those already familiar with the Win32API outside of Python...
printing out a text file with default printer
you don't have to use StringIO
for this. Just use the pipe feature of subprocess
and write your data to p.stdin
:
from subprocess import Popen
# call the system's lpr command
p = Popen(["lpr"], stdin=subprocess.PIPE, shell=True) # not sure you need shell=True for a simple command
p.stdin.write("test.txt")
output = p.communicate()[0]
as a bonus, this is Python 3 compliant (StringIO
has been renamed since :))
BUT: that would just print a big white page with one line: test.txt
. lpr
reads standard input and prints it (that's still an interesting piece of code :))
To print the contents of your file you have to read it, and in that case it's even simpler since pipe & files work right away together:
from subprocess import Popen
with open("test.txt") as f:
# call the system's lpr command
p = Popen(["lpr"], stdin=f, shell=True) # not sure you need shell=True for a simple command
output = p.communicate()[0]
Related Topics
How to Scroll Frame Using Mouse Wheel & Adding Horizontal Scrollbar
Log into Gmail Using Selenium in Python
How to Make Image/Images Disappear in Pygame
What Is _Future_ in Python Used for and How/When to Use It, and How It Works
How to Plot Multiple Functions on the Same Figure, in Matplotlib
Could Pandas Use Column as Index
How to Change Values in a Tuple
How to Print Bold Text in Python
How to Determine the Language of a Piece of Text
Find First Element in a Sequence That Matches a Predicate
Python Nested Functions Variable Scoping
Change User-Agent for Selenium Web-Driver
How to Find Numeric Columns in Pandas
How to Convert Columns into One Datetime Column in Pandas