ActiveState Code

Recipe 496837: Count PDF pages


A simple way to count the pages of a PDF the pure Python way.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import re

rxcountpages = re.compile(r"$\s*/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("test.pdf")

Discussion

Very straight forward approach. To do more counting and manipulation with PDF have a look at pyPDF (http://pybrary.net/pyPdf).

Sign in to comment