[Tutor] Printing in Windows (was: Further help needed!
carroll at tjc.com
Wed Jan 4 23:21:27 CET 2006
I've edited the subject line to be a little more clear.
On Wed, 4 Jan 2006, John Corry wrote:
> I am using the following code to send a text file to the printer:-
[ snip ]
> This code works on windows XP + Windows 2000. However it does not work on
> windows 98SE.
Well, at least that narrows it down. This continues to befuddle me.
I don't know enough about Windows to know the differences that might be
here for this purpose.
Question: do you need to use the Windows print function, or can you live
with printing it yourself (as below)?
For example, if you use the ShellExecute approach, it actually causes an
application to be invoked for you, which does the printing, just as if you
right-clicked on the file and selected "print." ON my system, for
example, that will invoke Notepad, which prints it with a heading and a
page number at the bottom.
But, if you don't care about that, assuming your file is straight text,
try the win32print approach from
http://tgolden.sc.sabren.com/python/win32_how_do_i/print.html . Here's an
attempt that seems to work for me:
lines_to_print = open("testprint.txt","r").readlines()
print_data = ''
for line in lines_to_print:
printer_name = win32print.GetDefaultPrinter()
printer = win32print.OpenPrinter(printer_name)
print_job = win32print.StartDocPrinter(printer, 1, ("Printer test", None, "RAW"))
The for-loop is a kludge; my first attempt was something like
but it turns out my printer needs a carriage return on each line.
I suspect that printer handling is more likely to be inconsistent between
Win98 and Win/XP than the ShellExecute you were already using, and so this
may not work; but it's worth looking into.
More information about the Tutor