How to count the number of pdf pages in python that has a blank pdf page

I tried to print an invoice of a pdf document that contains some blank white pdf page using the pypdf module. But he avoids the page spaces and prints the number of remaining pages. Below is the code.

import sys import pyPdf from pyPdf import PdfFileReader, PdfFileWriter pdf_document = PdfFileReader(file(normalpdfpath,"r")) normal = pdf_document.getNumPages() print normal 
+4
source share
1 answer

You can try this, which worked for me:

 import re import os rxcountpages = re.compile(r"/Type\s*/Page([^s]|$)", re.MULTILINE|re.DOTALL) def count_pages(filename): data = file(filename,"rb").read() return len(rxcountpages.findall(data)) if __name__=="__main__": parent = "/Users/username/" os.chdir(parent) filename = 'LaTeX20120726.pdf' print count_pages(filename) 

Hello

+2
source

Source: https://habr.com/ru/post/1481738/


All Articles