Lpr -module in Python

8k views Asked by At

How can you call lpr in Python?

It is not in the sys -module which is surprising.

I aim to use the lpr as follows shown by pseudo-code

10*i for i in range(77):              
      lpr --pages(i,i+1) file.pdf
3

There are 3 answers

0
Stephan202 On BEST ANSWER

First of, I don't understand your pseudo code. (What does 10*i for i in range(77): mean in this case?)

Generally, you use subprocess.Popen to run external commands. ActiveState recipe 511505 shows an example specifically with lpr. Basically, you can invoke lpr like this:

subprocess.Popen(['lpr', 'some_filename'])

However: Depending on your version of lpr, there may not be an option to select a subset of all pages, or this functionality may be available only for e.g. dvi files.

Edit: Since you seem to want to print selected pages of PDF files, have a look at the PDF toolkit. That software appears to provide splitting functionality. Also, make sure that directly printing PDF files is supported. You may need to convert the input to postscript first (e.g. using pdf2ps). Of course you can automate these tasks using subprocess.Popen as well.

0
Reinout van Rees On

Just call it from the commandline:

import commands

for i in range(77):
    # I'm making no assumptions about lpr command syntax here.
    cmd = "lpr --pages(%s,%s) file.pdf" % (2*i, 2*i+1)
    commands.getoutput(cmd)

Something like that.

2
niels On

I haven't tried it, but pycups appears to be python bindings for cups.

http://cyberelk.net/tim/software/pycups/