Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85724 > unrolled thread

Printing to printer, windows 7

Started byken.hessel@gmail.com
First post2015-02-16 18:08 -0800
Last post2015-02-18 02:11 +0000
Articles 7 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Printing to printer, windows 7 ken.hessel@gmail.com - 2015-02-16 18:08 -0800
    Re: Printing to printer, windows 7 Dave Angel <davea@davea.name> - 2015-02-16 22:02 -0500
    Re: Printing to printer, windows 7 Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-02-17 00:50 -0500
    Re: Printing to printer, windows 7 sjmsoft@gmail.com - 2015-02-17 12:10 -0800
    Re: Printing to printer, windows 7 Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-02-17 20:41 -0500
    Re: Printing to printer, windows 7 sohcahtoa82@gmail.com - 2015-02-17 17:59 -0800
    Re: Printing to printer, windows 7 MRAB <python@mrabarnett.plus.com> - 2015-02-18 02:11 +0000

#85724 — Printing to printer, windows 7

Fromken.hessel@gmail.com
Date2015-02-16 18:08 -0800
SubjectPrinting to printer, windows 7
Message-ID<c4b69693-c5fa-4c89-b4ab-a7a1fe898c22@googlegroups.com>
Would seem to be a simple problem.  I just want to print to my printer instead of the console using Python 2.7, Windows 7.  Hours of looking through FAQ's and Google haven't yielded a solution.  Any suggestions appreciated --

[toc] | [next] | [standalone]


#85728

FromDave Angel <davea@davea.name>
Date2015-02-16 22:02 -0500
Message-ID<mailman.18776.1424142167.18130.python-list@python.org>
In reply to#85724
On 02/16/2015 09:08 PM, ken.hessel@gmail.com wrote:
> Would seem to be a simple problem.  I just want to print to my printer instead of the console using Python 2.7, Windows 7.  Hours of looking through FAQ's and Google haven't yielded a solution.  Any suggestions appreciated --
>

It is a simple problem.  All you have to do is send the correct bytes to 
a file object that will transfer those bytes to the printer.  It's been 
a long time since I used Windows, but if I recall, you just open the 
device "prn"  or maybe "lpt1" and go.

For example, to print "Hello world" to an Epson MX80, on a local 
parallel port, you'd do

outfile = open("lpt1", "wb")
print >> outfile, "Hello world"

(To be polite, you should use   "\\dev\\lpt1", but I don't know whether 
the simpler form has ever been deprecated by Windows)

Unfortunately, printers have gotten increasingly complicated since the 
days of the MX80.  For example on a HP laser printer, it won't start 
rendering till you send a hex "0c" to it (also known as a formfeed). 
You also may need to control fonts, sizes, justifications, etc., and 
that varies by printer model. Some printers want Postscript.  So you are 
no longer talking to a printer device, but to a complex printer driver.

I'm afraid when I want to print something for the last decade or so, I 
just create some form of document, and use the corresponding program to 
print it for me.  For simple text, I put it in a simple text file, fire 
up emacs, and tell it to print.

If you've managed to get the equivalent of a "print-to-file" output for 
your particular printer, then you can print it by just copying the bytes 
in that file to the "prn" or "lpt1" device.

Depending on the data type you're trying to print, you may be able to 
control the external program from within Python.  For example, if you're 
trying to print a pdf file, I got the following fragment from a google 
search:

import win32api
fname="C:\\somePDF.pdf"
win32api.ShellExecute(0, "print", fname, None,  ".",  0)


In this fragment, you're asking the registered pdf viewer to print it 
for you.  (presumably Acrobat reader or the equivalent)

If you've got a Postscript printer, perhaps the following link will 
help, to print using PIL:
    http://effbot.org/imagingbook/introduction.htm


The conventional way to print to an arbitrary printer, assuming it's 
already installed to your OS, is to use one of the GUI libraries.  If 
your program is already GUI, then this is the way to go.  For example, 
see tkinter, wxpython, qt, ...

In this last case, the drivers and libraries abstract out the particular 
kind of printer, but you need a lot more software to use it.



-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#85732

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2015-02-17 00:50 -0500
Message-ID<mailman.18780.1424152259.18130.python-list@python.org>
In reply to#85724
On Mon, 16 Feb 2015 18:08:22 -0800 (PST), ken.hessel@gmail.com declaimed
the following:

>Would seem to be a simple problem.  I just want to print to my printer instead of the console using Python 2.7, Windows 7.  Hours of looking through FAQ's and Google haven't yielded a solution.  Any suggestions appreciated --

	It hasn't been easy since W9x...

	Windows printers aren't considered text output devices; one has to
basically create a graphical image of the page and send the image to the
printer driver. That means lots of win32 function calls on graphical
contexts, rendering, etc.

	Simplest is to just write to a scratch/temp text file, and invoke some
utility to do the printing. I used to know how to invoke Word to
start/print/exit...

... Just tested

C:\Users\Wulfraed\Documents>notepad /p test.csv

C:\Users\Wulfraed\Documents>

did print the file (but put a header line of the file name at the top --
might be an option to suppress that). Other programs may respond to a /p as
a command to print the file. (Appears SciTE takes it, different header and
footer -- the SciTE help says it is -p to print the file)

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#85751

Fromsjmsoft@gmail.com
Date2015-02-17 12:10 -0800
Message-ID<080efc88-1a16-4f88-85ca-828579795ffc@googlegroups.com>
In reply to#85724
On Monday, February 16, 2015 at 10:08:33 PM UTC-4, ken.h...@gmail.com wrote:
> Would seem to be a simple problem.  I just want to print to my printer instead of the console using Python 2.7, Windows 7.  Hours of looking through FAQ's and Google haven't yielded a solution.  Any suggestions appreciated --

There is a good discussion on Tim Golden's site:

http://timgolden.me.uk/python/win32_how_do_i/print.html

As he notes, alas, there is a "disparity between the number and complexity of solutions and the simplicity of the requirement."

HTH,
  Steve J. Martin

[toc] | [prev] | [next] | [standalone]


#85762

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2015-02-17 20:41 -0500
Message-ID<mailman.18800.1424223693.18130.python-list@python.org>
In reply to#85724
On Mon, 16 Feb 2015 22:02:33 -0500, Dave Angel <davea@davea.name> declaimed
the following:


>It is a simple problem.  All you have to do is send the correct bytes to 
>a file object that will transfer those bytes to the printer.  It's been 
>a long time since I used Windows, but if I recall, you just open the 
>device "prn"  or maybe "lpt1" and go.
>

C:\Users\Wulfraed\Documents>copy t.txt prn:
"prn:" is not a recognized device.
The system cannot find the file specified.

C:\Users\Wulfraed\Documents>copy t.txt lpt1:
"lpt1:" is not a recognized device.
The system cannot find the file specified.

	And...

C:\Users\Wulfraed\Documents>copy t.txt CN3BSFVGP805ZZ:
"CN3BSFVGP805ZZ:" is not a recognized device.
The system cannot find the file specified.

	That's the port name associated with my HP printer when looking at the
control panel properties.

	LPT1: appears to be linked to "Quicken PDF Printer" in the port list,
but is not available from command line.

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#85763

Fromsohcahtoa82@gmail.com
Date2015-02-17 17:59 -0800
Message-ID<d02c0781-2e62-4f79-9b55-bcff930cdf86@googlegroups.com>
In reply to#85724
On Monday, February 16, 2015 at 6:08:33 PM UTC-8, ken.h...@gmail.com wrote:
> Would seem to be a simple problem.  I just want to print to my printer instead of the console using Python 2.7, Windows 7.  Hours of looking through FAQ's and Google haven't yielded a solution.  Any suggestions appreciated --

I know that most HP printers support connecting on TCP port 9100 and sending plain text to be printed.  Not sure about other brands.  If you want to get fancy, you can look up PJL/PCL to do text formatting.

Of course, this requires you to know the IP address of the printer.  I wouldn't know how to get that programmatically.

[toc] | [prev] | [next] | [standalone]


#85765

FromMRAB <python@mrabarnett.plus.com>
Date2015-02-18 02:11 +0000
Message-ID<mailman.18801.1424225701.18130.python-list@python.org>
In reply to#85724
On 2015-02-18 01:41, Dennis Lee Bieber wrote:
> On Mon, 16 Feb 2015 22:02:33 -0500, Dave Angel <davea@davea.name> declaimed
> the following:
>
>
>>It is a simple problem.  All you have to do is send the correct bytes to
>>a file object that will transfer those bytes to the printer.  It's been
>>a long time since I used Windows, but if I recall, you just open the
>>device "prn"  or maybe "lpt1" and go.
>>
>
> C:\Users\Wulfraed\Documents>copy t.txt prn:
> "prn:" is not a recognized device.
> The system cannot find the file specified.
>
> C:\Users\Wulfraed\Documents>copy t.txt lpt1:
> "lpt1:" is not a recognized device.
> The system cannot find the file specified.
>
> 	And...
>
> C:\Users\Wulfraed\Documents>copy t.txt CN3BSFVGP805ZZ:
> "CN3BSFVGP805ZZ:" is not a recognized device.
> The system cannot find the file specified.
>
> 	That's the port name associated with my HP printer when looking at the
> control panel properties.
>
> 	LPT1: appears to be linked to "Quicken PDF Printer" in the port list,
> but is not available from command line.
>
You can use the "NET USE" command to make LPT1: refer to your printer.
The details are on the Web. :-)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web