count number of pages in pdf python pdfminer 1

count number of pages in pdf python pdfminer

from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfinterp import resolve1

file = open('some_file.pdf', 'rb')
parser = PDFParser(file)
document = PDFDocument(parser)

# This will give you the count of pages
print(resolve1(document.catalog['Pages'])['Count'])

Here is what the above code is Doing:
1. Open the PDF file
2. Create a parser object
3. Create a PDFDocument object that stores the document structure
4. Get the number of pages in the document

Similar Posts