Printing on a Windows workstation
Andy Robinson
andy at robanal.demon.co.uk
Sun Nov 7 17:06:35 EST 1999
"Robert Parker" <rparker at bserv.com> wrote:
>Hi All
> I have waded through the man pages and the functions to try and
>determine what to use to print to a printer using Win32. I don't know if
>there is a COM that could be used or some other method to access the printer
>API in Win32. I don't feel excited about having to generate lines of escape
>sequences to accomplish formatting and proper presentation. Does any one
>have any suggestions?
>
> As a newbie it sure is helpful having this newsgroup. Am greatful for
>any assistance.
>
>Robert Parker-)
<plug>
In the forthcoming "Python Programming for Win32", I have a chapter on
printing under Windows. Broadly I cover three suggestions:
- directly drawing to a printer canvas (Pythonwin does exactly what
you want, just like any other Windows graphics code)
- automating Word
- generating PDF
</plug>
Since it is not out yet, You can find some code examples for the Word
stuff at www.robanal.demon.co.uk/PythonWindowsTutorial.pdf
And you should look at PIDDLE (www.strout.net/python/piddle), which
includes the PDF-generating library. It's quite easy to write scripts
to do any report you want as a PDF document, although the recipient
has to open it and hit 'print'.
If you want to write directly to a Windows device context, try the
snippet here:
========simpleprintdemo.py==============================
# basic printing demonstration
import win32ui
import win32con
myprinter = "HP DeskJet 820C Series Printer"
# get the name from your Printers folder
def print_it():
dc = win32ui.CreateDC()
dc.CreatePrinterDC() # ties it to your default printer
dc.StartDoc('My Python Document')
#dc.SetMapMode(win32con.MM_LOENGLISH)
dc.StartPage()
# text - near the top left corner somewhere
dc.TextOut(110,790, 'Hello, World') # 1 inch in, 8 up
# try to draw a box around it - not device-specific
dc.MoveTo(100,800)
dc.LineTo(400,800)
dc.LineTo(400,700)
dc.LineTo(100,700)
dc.LineTo(100,800)
dc.EndPage()
dc.EndDoc()
print 'sent to printer'
del dc
=========================================================
The thing I did not cover, because it works but is too ugly for
real-world reports, is that you can print a text file (or Postscript
file if that is what you have) with a DOS command like
copy myfile.txt lpt1:
and of course can automate this with a Python call to os.system().
Hope this helps.
Andy
More information about the Python-list
mailing list