1

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 Answers3

5

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.

Stephan202
  • 59,965
  • 13
  • 127
  • 133
2

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.

Reinout van Rees
  • 13,486
  • 2
  • 36
  • 68
0

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

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

Eran
  • 387,369
  • 54
  • 702
  • 768
niels
  • 635
  • 5
  • 4
  • 7
    To save time for anyone reading this: pycups lets you manage printers and queues, not print to them. – Nick Johnson Sep 25 '13 at 16:09
  • @NickJohnson More specifically, the API would appear to indicate that you can [print files present on the local filesystem](http://pythonhosted.org/pycups/cups.Connection-class.html#printFile), but you cannot stream data to a printer. This means pycups is definitely not a stand-in for lpr if you are not looking to create temporary files, which seems to be an [intentional exclusion](https://lists.fedorahosted.org/pipermail/system-config-printer-devel/2013-December/000157.html). – Jmills Aug 18 '15 at 19:53