git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Print to Paper


Daniel <duram at terra.com.br> writes:

> How to do a code to print to paper?  please post here a "Hello World"
> code to be printed on paper with an inkjet.
> Thanks
> Daniel

It depends on the Operating System. Python doesn't have a standard, system-independent way to print to paper.
On most Unix-like systems (like my MacOS) the following works:

from subprocess import Popen, PIPE
p = Popen('lpr', stdin=PIPE)
p.communicate(b'Hello World')
p.stdin.close()
p.wait()

In a production system error checking should be added. Also if a nice layout is desired, you should probably want to generate a PDF file and send that to the printer, possibly with the use of LaTeX.
-- 
Piet van Oostrum <piet-l at vanoostrum.org>
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]