python get PDF page count

7559
3
12-05-2011 08:15 AM
RyanKelley
New Contributor II
Having a tough time finding an easy answer to this. We have Report Lab, but haven't had much success getting at this piece. Seems like it should be very easy...

I am appending multiple PDFs and am writing out a TOC for a cover page, but need the number of pages in each PDF I am appending. Any thoughts?

thank you,
Ryan
Tags (2)
0 Kudos
3 Replies
RyanKelley
New Contributor II
ok, an answer and something that doesn't need some crazy function... Also, the pyPdf library can give you results in one line of code apparently, which I didn't try.

pdf = "my.pdf"

import re

rxcountpages = re.compile(r"/Type\s*/Page([^s]|$)", re.MULTILINE|re.DOTALL)

def countPages(filename):
    data = file(filename,"rb").read()
    return len(rxcountpages.findall(data))

if __name__=="__main__":
    print "Number of pages in PDF File:", countPages(pdf)
0 Kudos
JeffBarrette
Esri Regular Contributor
Hi Ryan,

Have you tried the pageCount property on the PDFDocument object?

pdfDoc = arcpy.mapping.PDFDocumentOpen(path)
print pdfDoc.pageCount


Jeff
0 Kudos
RyanKelley
New Contributor II
Hi Jeff,

Why of course not... that would have saved me too much time. Not sure how I missed that one...

many thanks,
ryan
0 Kudos