Getting in python in advance the number of pages after printing a document


My printer does not support two-sides printing, therefore I make a simple script in Python based on lpr utility. It prints documents page by page with delays, during which I'm turning papers manually:

from sys import argv
from subprocess import run

assert(len(argv) == 2)

i = 1
  while True:
    opt = 'page-ranges={}'.format(i)
    i += 1
    print(run(['lpr', '-o', opt, argv[1]]))
    f = input('> Ready to print next page?\n'
              '(Press ENTER or CTRL+C to exit) ')
except KeyboardInterrupt:

The problem is I don't know in advance the number of pages of the final printed document, so I don't know when I should interrupt while loop.

Maybe is there solution that helps me get number of resulting pages?

P.S. I'm using macOS Sierra 10.12.2 and A4 papers.

Show source
| osx   | python   | unix   | printing   | printers   2017-01-05 19:01 1 Answers

Answers ( 1 )

  1. 2017-01-05 20:01

    Counting pdf

    from pyPdf import PdfFileReader
    pdf = PdfFileReader(open('path/to/file.pdf','rb'))

    Counting docs

    from win32com.client import Dispatch
    #open Word
    word = Dispatch('Word.Application')
    word.Visible = False
    word = word.Documents.Open(doc_path)
    #get number of sheets
    num_of_sheets = word.ComputeStatistics(2)

    refer the below pages for more details thanks to @Josh & @a_guest

    pyPDF - Retrieve page numbers from document

    Number of pages of a word document with Python

◀ Go back