How to Get the Count of Number of Pages in a PDF File in Python?

When dealing with a PDF file in our regular lifestyle, we may need to know how many pages there are.

If the page number is out of bounds, you may receive an error while accessing any page or anything from the PDF. We can count the number of pages in a PDF file or Get number of pages in Pdf Python to avoid these types of problems, Count Number Of Pages In Pdf Python, Python Pdf Page Count,

PyPDF2 library:
Python includes a variety of built-in functions. To count the pages of a PDF file, we can use the Python inbuilt library ‘PyPDF2’ Pypdf2 Get Number Of Pages, Pypdf2 Number Of Pages, Python Count Pdf Pages, Python Get Pdf Page Count, Python Get Number Of Pages In Pdf, Python Pdf Number Of Pages, Python Count Pages In Pdf, Count Pdf Pages Python, Pypdf2 Count Pages, Fitz Get Number Of Pages, Pymupdf Get Number Of Pages, Pdf Page Count Python, Pypdf2 Get Page Number, Python Read Pdf, Read Pdf Python, Fitz Page Count processes are mentioned below.
Before we work with the module PyPDF2 we should first install it.
Installation:
pip install PyPDF2

Output:

Collecting PyPDF2
Downloading PyPDF2-1.26.0.tar.gz (77 kB)
|████████████████████████████████| 77 kB 2.8 MB/s 
Building wheels for collected packages: PyPDF2
Building wheel for PyPDF2 (setup.py) ... done
Created wheel for PyPDF2: filename=PyPDF2-1.26.0-py3-none-any.whl size=61102 sha256=818687400c96b3b
bcd3f38aecf0870e60e7c5241f0c6aa10b8fc45ffb26de2f1
Stored in directory: /root/.cache/pip/wheels/80/1a/24/648467ade3a77ed20f35cfd2badd32134e96dd25c
a811e64b3
Successfully built PyPDF2
Installing collected packages: PyPDF2
Successfully installed PyPDF2-1.26.0

Count of Number of Pages in a PDF File in Python

Below are the ways to count the number of pages in the given pdf file:

Method #1: Using PyPDF2 module

Approach:

  • Import PyPDF2 module using the import keyword
  • Open the PDF file in read-binary mode(converts file into binary format) using the open()
    function and store it in a variable.
  • Pass the above PDF file to the PdfFileReader() function to read the pdf file and store it in another variable.
  • Apply numPages attribute on the above read pdf to count the total number of pages in the given PDF file and store it in another variable.
  • Print the count of the total number of pages in the given PDF file.
  • The Exit of the Program.

Below is the implementation:

# Import PyPDF2 module using the import keyword
import PyPDF2
# Open the PDF file in read-binary mode(converts file into binary format) using the open()
# function and store it in a variable.
gvn_file = open('btechgeeks.pdf', 'rb')
# Pass the above PDF file to the PdfFileReader() function to read the pdf file and
# store it in another variable.
pdf_read = PyPDF2.PdfFileReader(gvn_file)
# Apply numPages attribute on the above read pdf to count the total number of pages in
# the given PDF file and store it in another variable.
pages_count = pdf_read.numPages
# Print the count of total number of pages in the given PDF file
print("The total number of pages in the given PDF file = ", pages_count)

Output:

The total number of pages in the given PDF file = 8

Method #2: Using pymupdf module

First, install the pymupdf module before going to the code given below:

pip install pymupdf

Approach:

  • Import fitz function using the import keyword
  • Open the PDF file using the open() function and store it in a variable.
  • Apply pageCount on the above pdf file to get the count of total number of pages in a given PDF file and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import fitz function using the import keyword
import fitz
# Open the PDF file using the open() function and store it in a variable.
gvn_pdffile = fitz.open('btechgeeks.pdf')
# Apply pageCount on the above pdf file to get the count of total number of 
# pages in a given PDF file and print the result.
print("The total number of pages in the given PDF file: ")
gvn_pdffile.pageCount

Output:

The total number of pages in the given PDF file: 
8

Recommended Reading On: How to Find the Page Number of a Text from a PDF File in Python?