[Tutor] Windows Printing, Round 2
ranceh at gmail.com
Thu Sep 23 20:18:33 CEST 2010
Again I'm referencing Tim Golden from
This code block is relevant:
import os, sys
printer_name = win32print.GetDefaultPrinter ()
# raw_data could equally be raw PCL/PS read from
# some print-to-file operation
if sys.version_info >= (3,):
raw_data = bytes ("This is a test", "utf-8")
raw_data = "This is a test"
hPrinter = win32print.OpenPrinter (printer_name)
hJob = win32print.StartDocPrinter (hPrinter, 1, ("test of raw data",
win32print.WritePrinter (hPrinter, raw_data)
Things are progressing along and I'm really more and more excited
about python, every time I try to do something, It just seems to work
and be straightforward.
Notes: AFAICS win32pring.StartDocPrinter(hPrinter, 1, (jobname, None,
None)) might be better as the last "None" tells the system to process
through the print driver, Raw Data bypasses the print driver.
Due to the variety of printers involved, I think bypassing the print
driver with "RAW" will come back and bite me later.
I also added a variable to catch the output from
win32print.WritePrinter() so it would not display on the screen. I
called it hSize.
Under python 3 I need to send a byte string to the printer. But I
wont have a byte string, I'll have a filename
What is the pythonic way to convert a file to a bytestream?
I can open the file for reading, and loop line by line through the
file appending bytes(line, "utf-8") to variable but this doesn't seem
right to me somehow.
Is there a better way to do this?
My HP laserjet 1100 does not print the job automatically. It accepts
the job, processes the job, and then lights up the lights on the front
of the printer and waits. When I hit the button, then the document
I have only seen this behavior before when printing envelopes. When
an envelope print job goes to the printer it behaves the same way my
python print jobs are.
I suspect that this has to do with the fact that the page size of the
printjob is either not specified or different from the standard 8.5 in
wide x 11 in long it can handle.
win32print documentation mentions DocumentProperties and
DeviceCapabilities that might help, but I don't see how to use them to
implement a solution for my problem.
I further suspect that there are other printers out there that will
behave similarly if they don't have specified what they needed.
How do you deal with this problem?
More information about the Tutor