Printing from python on Windows platform

Andy Robinson andy at robanal.demon.co.uk
Tue Aug 10 15:53:53 EDT 1999


Johann Spies <jhspies at futurenet.co.za> wrote:

>I am a Linux user writing a program for somebody that will use it on a
>Windows platform.  In Linux I use the following to redirect printing to
>the printer:
>
>    drukker = sys.stdout
>    drukker = os.popen('lpr','w')
>    drukker.write(drukstuk)
>    sys.stdout.write(drukstuk)
>
>What would the windows equivalent be?
>
>Johann

It depends very much how your printer is set up.    First of all, it
matters what you want to send.  If you have a Postscript file and
Postscript printer, or only want to print text with line breaks, then
you can write directly to it.  Thus with a Postscript printer on the
port LPT3, the DOS command
	COPY MYFILE.PS LPT3:
will do the job.

With a network printer, it depends what OS you are on.  On NT, you can
install Microsoft TCPIP Printing Services (Control Panel - Network -
Services - Add - Microsoft TCPIP Printing Services), whcih gives you a
good LPR command.   Having done that, you can also create new ports
mapped to IP addresses, to which files can be written.

If you really want to use the Windows print queue management features,
you need to use the WritePrinter API call.  Instead of sending it
straight to the printer, the job goes to the NT server which manages
the printer, letting people cancel the job later.  But you need to
write C to handle that stuff.

Most Windows apps don't spool a pre-existing file; they create a
printer device context and draw on it, which works on all versions on
Windows.  If you are working in Pythonwin, I can send you a code
snippet which does this.

Let me know what you want to do - I've spent a lot of the last six
months getting Windows to print stuff securely in the middle of the
night!

- Andy











More information about the Python-list mailing list