How do i print to a printer using python?

Terry Reedy tjreedy at udel.edu
Thu Sep 5 00:48:41 EDT 2002


"Naveed Iqbal" <iqbal at colorado.edu> wrote in message
news:6afb9d2c.0209041044.43c5f5f1 at posting.google.com...
> I have to print to a standard printer. I take the data from the
> keyboard and print it using the printer. Please help me....my job
> depends on this!!!!

On *nix or dos/windows, you open the printer port as if it were a
file, just like other ports and devices.  On *nix, this maybe be
something like '/dev/lpt' or a the numbered serial or parallel port
that your printer is attached to.  On Dos and my win98 machine, this
does the trick:

>>> p=file('LPT1:','w')
>>> p.write('Test Line\n\f')
>>> p.flush() # p.close() also flushes

and out come a sheet with 'Test Line' printed.  Note that \f is
formfeed char; without it, nothing is printed on page printers (*jets,
etc.).

Terry J. Reedy







More information about the Python-list mailing list