[reportlab-users] Number of pages in a pdf file

reportlab-users@reportlab.com reportlab-users@reportlab.com
Wed, 11 Jun 2003 14:07:17 +0200


> *given that the files are always
> created using Reportlab*, is there a relatively simply and reliable way
to
> extract this information?

The best way I can find is to look for lines of the form:
     'PageXX' '<reportlab.pdfbase.pdfdoc.PDFPage instance '

import re

pageNumberRegexp = re.compile("% 'Page([0-9]+)'.+PDFPage ")
file = open(fileName)
for line in file:
  match = pageNumberRegexp.match(line)
  if match:
    pageNumber = match.group(1)

print pageNumber


You could also take the /Count value of the "page tree",
but it is not as easy to parse.

--
Amaury Forgeot d'Arc