[reportlab-users] Count pages in PDF?

Dinu Gherman reportlab-users@reportlab.com
Wed, 17 Mar 2004 12:44:00 +0100


Chris Withers:

> Is there anything reportlab-ish that can quickly count
> the number of pages in a PDF?

The simple answer is "No." The more complicated one is left
for others...

> If not, how do people do that?

With the sample code below... (only needs a Mac and PyObjC ;-)

Dinu


#!/usr/bin/env python

"pdfpagecount.py - print number of pages in a PDF file."

from Foundation import NSData
from AppKit import NSPDFImageRep

def pageCount(pdfPath):
     "Return the number of pages for some PDF file."
     data = NSData.dataWithContentsOfFile_(pdfPath)
     img = NSPDFImageRep.imageRepWithData_(data)
     return img.pageCount()

if __name__ == '__main__':
     import sys
     if len(sys.argv) == 2:
         print pageCount(sys.argv[1])