[Tutor] Windows Printing, Round 2

Tim Golden mail at timgolden.me.uk
Thu Sep 23 21:15:41 CEST 2010


On 23/09/2010 7:18 PM, Rance Hall wrote:
> Again I'm referencing Tim Golden from
>
> http://timgolden.me.uk/python/win32_how_do_i/print.html
>
>
> This code block is relevant:
>
> <code>
>
> import os, sys
> import win32print
> 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")
> else:
>    raw_data = "This is a test"
>
> hPrinter = win32print.OpenPrinter (printer_name)
> try:
>    hJob = win32print.StartDocPrinter (hPrinter, 1, ("test of raw data",
> None, "RAW"))
>    try:
>      win32print.WritePrinter (hPrinter, raw_data)
>    finally:
>      win32print.EndDocPrinter (hPrinter)
> finally:
>    win32print.ClosePrinter (hPrinter)
>
> </code>
>
>
>
> 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.
>
> Questions:
>
> 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
> prints.
>
> 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?

Essentially the complexity of the answer to this question -- the big
gap between raw (textish) data and any other formatted output -- was
what prompted my earlier suggestion to use IE as a print engine.

An easy answer to your questions above would be:

send the file's byte contents (see snippet below) followed by a formfeed
to prompt the printer into actually doing something.

<code>
import os, sys
import win32print
printer_name = win32print.GetDefaultPrinter ()  # for simplicity

raw_data = open ("filename.txt", "rb").read () + b"\x0c"

# OpenPrinter / ClosePrinter dance as above

</code>

Does that help?

TJG


More information about the Tutor mailing list