[Pythonmac-SIG] Sample code counting PDF pages

Dinu Gherman gherman at darwin.in-berlin.de
Wed Mar 17 06:50:28 EST 2004


Hi,

somebody on the ReportLab list just asked how to count the pages
of a PDF file using ReportLab. I used the chance for a shameless
plug and wrote this quickie he can use on a Mac with PyObjC and
without ReportLab.

Just a recipe for the records...

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])




More information about the Pythonmac-SIG mailing list