How to count the number of pages in a PDF file in Python
We will discuss how to count the number of pages in a PDF file in Python here. In our daily uses, while working with a PDF file, we may need to know how many pages are there.
While accessing any page or something from the PDF, you may get an error if the page number is out of bound. To avoid these types of unwanted errors, we may count the number of pages in a PDF file.
To count the number of pages in a PDF file
Python supports many inbuilt functions. We can use a python inbuilt package named ‘PyPDF2’ to count PDF file pages.
If you do not have the ‘PyPDF2’ package in your system for python. You need to install it first.
pip install PyPDF2
The above command will install the package and the necessary functions to work with.
We will count the number of pages in a PDF file using some simple steps:
Step 1: Import the package ‘PyPDF2’ in Python.
Step 2: Open the PDF file and convert it into binary format.
Step 3: Read the PDF file using the imported package format.
Step 4: Read the PDF page numbers and store them in a variable.
Step 5: Display the number of pages in the PDF file.
Suppose we have taken a PDF containing 34 pages and saved it as ‘CodeSpeedy.pdf’.
Then we can run the code given below:
import PyPDF2 file = open('CodeSpeedy.pdf', 'rb') readpdf = PyPDF2.PdfFileReader(file) totalpages = readpdf.numPages print(totalpages)
Here, the ‘open()’ function will open the given PDF file and there ‘rb’ will read and convert it in binary format. The ‘PdfFileReader()’ is an inbuilt function from the package ‘PyPDF2’. ‘numPages’ will count the page numbers from the given PDF file. And the ‘print()’ function will display the result.
You may also read:
Check if a string exists in a PDF file