Tim Golden > Python Stuff > Win32 How Do I...? > Print
Introduction
The requirement: to print
This is probably the most wide-ranging question I'll have to address here, and the one with the greatest disparity between the number and complexity of solutions and the simplicity of the requirement. The answer is: it all depends what you're trying to print, what tools you have at your disposal, and how much control you need.
If you simply have a "document" (read: file of a well-known type, associated with one application) you wish to print, and aren't too fussy about controlling, then you can use the ShellExecute approach. This works (assuming you have the corresponding applications installed) for Microsoft Office documents, PDF files, text files, and pretty much any major application. Try it and see.
The next most general case is where you have something, for example a text file or raw PCL, which you know you can send directly to a printer. In that case, you can use the win32print functions directly.
If you have an image to print, you can combine the power of the Python Imaging Library with the win32ui module to do a rough-and-ready but useful print to any printer.
If you have a fair amount of text to print, your best bet is to use the Reportlab PDF Toolkit and its Platypus document system to generate readable PDFs from any amount of text, and then use the ShellExecute technique to print it.
Standard document: use ShellExecute
Make use of the fact that within Win32, file types (in effect, extensions) can be associated with applications via command verbs. Typically the same application will handle all verbs (and typically those verbs are Open and Print) but that's not strictly necessary. This means that you can tell the OS to take your file and call whatever's necessary to print it.
Takes care of standard file types
No need to mess around with printer lists
Gives you no control
Only works for well-defined document-application pairings.
Only prints to defa