printing HTML or PDF from Python

Thomas Heller theller at python.net
Wed Oct 27 03:54:33 EDT 2004


Donnal Walter <donnal at donnal.net> writes:

> We want to be able to print HTML or PDF files to a given printer from
> Python in a kind of batch mode (without opening a preview window or
> printer dialog for each file). The printer is on a network, but for
> testing purposes I have connected it to my desktop via LPT1. I can
> easily print a text file from Python using:
>
> os.system("copy some.txt LPT1")
>
> And as expected, using this function with "some.html" simply prints
> the html source. (But I tried it anyway. :-) I presume that one would
> need to go through a Web browser of some kind to print the formatted
> document (using CSS, BTW), but we don't want the user to see the
> browser window, or be required to click on the Print button, etc. Is
> there a way to do this from a Python script? We are currently deployed
> on Windows, but would like to keep the application cross-platform if
> possible. We have Mozilla installed, for example, if that would be of
> use, but I've searched the Mozilla mailing list archives without
> seeing anything about printing via Mozilla without actually opening a
> browser window.
>
> The same more or less goes for PDF files. We have Acrobat Reader
> installed, but I can't see how to print a PDF file from Python via AR
> without opening the file inside the reader and manually printing each
> one. Any suggestions would be much appreciated.

On windows, you would use the ShellExecute function, with 'print' as the
second parameter.  Available in pywin32.  Python itself also exposes it
as the os.startfile function, but unfortunately this doesn't accept a
second parameter.

Thomas



More information about the Python-list mailing list