---
title: Count the number of pages in a PDF file with Python and PyPDF2
description: Count the number of pages in a PDF file with Python and PyPDF2
---

[Conny Söderholm](https://www.connysoderholm.com/blog)

# [Count the number of pages in a PDF file with Python and PyPDF2](https://www.connysoderholm.com/blog/count-the-number-of-pages-in-a-pdf-file-with-python-and-pypdf2)

 Written by [Conny Söderholm](https://www.connysoderholm.com/blog/author/conny-soderholm) | August 19, 2020

## Counting the number of PDF pages

Do you need to count the total number of pages in your pdf files?

Look no further, as **PyPDF2** is here to help you!

Here is the documentation: [https://pythonhosted.org/PyPDF2/](https://pythonhosted.org/PyPDF2/)

To count the number of pages in a PDF file, you need only four lines of code.

You can install PyPDF2 with pip ([PyPI](https://pypi.org/project/PyPDF2/)):

```
py -m pip install PyPDF2
```

## The code

Ok ok enough installing, what do we need to do to count the pages?

First, we want to import the PdfFileReader class from PyPDF2

`from PyPDF2 import PdfFileReader`

After that, we need to open our PDF file in binary reading mode.

`with open("your_pdf_file.pdf", "rb") as pdf_file:`

We then want to instantiate our PdfFileReader object

`pdf_reader = PdfFileReader(pdf_file)`

We then get the number of pages with the numPages property

`print(f"The total number of pages in the pdf document is {pdf_reader.numPages}")`

That's it! We have now counted the number of pages in a PDF file with Python!

## The complete code:

```
#!/usr/bin/env python3

"""
Extracting number of pages in the document

getNumPages()
Calculates the number of pages in this PDF file.

Returns: number of pages
Return type: int
Raises PdfReadError:
if file is encrypted and restrictions prevent this action.

numPages
Read-only property that accesses the getNumPages() function.
"""

from PyPDF2 import PdfFileReader

# Load the pdf to the PdfFileReader object with default settings
with open("your_pdf_file.pdf", "rb") as pdf_file:
pdf_reader = PdfFileReader(pdf_file)
print(f"The total number of pages in the pdf document is {pdf_reader.numPages}")
```

 

[View full post](https://www.connysoderholm.com/blog/count-the-number-of-pages-in-a-pdf-file-with-python-and-pypdf2)

```json
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Conny Söderholm"
  },
  "dateModified" : "2023-07-31T14:31:15.019Z",
  "datePublished" : "2020-08-19T19:46:15Z",
  "headline" : "Count the number of pages in a PDF file with Python and PyPDF2",
  "image" : {
    "@type" : "ImageObject",
    "height" : 60,
    "url" : "/hs/hsstatic/content_shared_assets/static-1.4092/img/default-amp-logo.png",
    "width" : 60
  },
  "mainEntityOfPage" : "https://www.connysoderholm.com/blog/count-the-number-of-pages-in-a-pdf-file-with-python-and-pypdf2",
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "height" : 60,
      "url" : "/hs/hsstatic/content_shared_assets/static-1.4092/img/default-amp-logo.png",
      "width" : 60
    },
    "name" : "Conny Söderholm"
  }
}
```