Select to view content in your preferred language

python get PDF page count

8278
3
12-05-2011 08:15 AM
RyanKelley
Deactivated User
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
Deactivated User
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
Deactivated User
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