How to write to printer in Windows?

gregholmes at my-deja.com gregholmes at my-deja.com
Wed Apr 19 06:15:48 EDT 2000


  Albert Wagner <alwagner at tcac.net> wrote:
> How can I write(aLine) to a printer in Windows?  I tried this:
>
> from win32pipe import popen
> :
> self.printer = popen('lpt1' 'w')
> :
> self.printer.write(aLine)
>
> but get a general protection fault.  Any help will be greatly
> appreciated.
>
> --
> Small is Beautiful
>
Here is a snippet of how I did it:

import win32ui
import win32con
self.dc = win32ui.CreateDC()
self.dc.CreatePrinterDC()   # ties it to your default printer
horzres=self.dc.GetDeviceCaps(win32con.HORZRES)
vertres=self.dc.GetDeviceCaps(win32con.VERTRES)
self.printxscale=(horzres/300)*0.8
self.printyscale=(vertres/300)*0.8
self.redpen = win32ui.CreatePen(win32con.PS_SOLID, 5, self.red)
self.blackpen = win32ui.CreatePen(win32con.PS_SOLID, 5, self.black)
self.dc.StartDoc('Graph')
#self.dc.SetMapMode(win32con.MM_LOENGLISH)
self.dc.StartPage()
self.dc.SelectObject(self.blackpen)
self.dc.SetTextAlign(win32con.TA_CENTER|win32con.TA_BASELINE)
self.dc.MoveTo((0+self.xoffset)*self.printxscale,10*self.printyscale)
self.dc.LineTo((0+self.xoffset)*self.printxscale,210*self.printyscale)
self.dc.MoveTo((0+self.xoffset)*self.printxscale,210*self.printyscale)
self.dc.LineTo((200+self.xoffset)*self.printxscale,210*self.printyscale)
self.dc.MoveTo((100+self.xoffset)*self.printxscale,210*self.printyscale)
self.dc.LineTo((100+self.xoffset)*self.printxscale,220*self.printyscale)
self.dc.MoveTo((50+self.xoffset)*self.printxscale,210*self.printyscale)
self.dc.LineTo((50+self.xoffset)*self.printxscale,220*self.printyscale)
self.dc.MoveTo((150+self.xoffset)*self.printxscale,210*self.printyscale)
self.dc.LineTo((150+self.xoffset)*self.printxscale,220*self.printyscale)
self.dc.MoveTo((0+self.xoffset)*self.printxscale,110*self.printyscale)
self.dc.LineTo((0+self.xoffset-10*self.printxscale,110*self.printyscale)
self.dc.MoveTo((0+self.xoffset)*self.printxscale,10*self.printyscale)
self.dc.LineTo((0+self.xoffset-10)*self.printxscale,10*self.printyscale)
self.dc.MoveTo((200+self.xoffset)*self.printxscale,210*self.printyscale)
self.dc.LineTo((200+self.xoffset)*self.printxscale,220*self.printyscale)
self.dc.MoveTo((0+self.xoffset)*self.printxscale,160*self.printyscale)
self.dc.LineTo((0+self.xoffset-10*self.printxscale,160*self.printyscale)
self.dc.MoveTo((0+self.xoffset)*self.printxscale,60*self.printyscale)
self.dc.LineTo((0+self.xoffset-10)*self.printxscale,60*self.printyscale)
-------
That prints some lines (the axes of a graph).

To do text you can do:
--------------
self.dc.TextOut
((100+self.xoffset*self.printxscale,230*self.printyscale, "some text")
---------------

To end the page, the document, and clean up:
--------------
self.dc.EndPage()
self.dc.EndDoc()
print 'sent to printer'
del self.dc
-----------------

Hope this helps.


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list