DEV Community

Cover image for How to Get the Total Number of PDF Pages with PyMuPDF
PDF Python Hub
PDF Python Hub

Posted on Originally published at payhip.com

How to Get the Total Number of PDF Pages with PyMuPDF

Once you have opened a PDF with PyMuPDF, you can easily find out how many pages it contains.

1. Using page_count

page_number = doc.page_count
print(page_number)
Enter fullscreen mode Exit fullscreen mode

The page_count property returns the total number of pages in the PDF.

Here, the number of pages is stored in the variable page_number, and print() displays it.

For example, if the PDF contains 44 pages, the output will be:
44

2. Using len()

You can also use Python's built-in len() function:
len(doc)

This returns the total number of pages in the PDF as well.

For example:

print(len(doc))
Enter fullscreen mode Exit fullscreen mode

Example Output:
44

Key takeaway
Both approaches give you the number of pages:
doc.page_count
or
len(doc)

Complete Example

# Method 1

page_number = doc.page_count
print(page_number)

# Method 2

print(len(doc))
Enter fullscreen mode Exit fullscreen mode

Open the notebook

Open the Google Colab notebook for this PDF mini-guide and run the code as you follow along.

[Open in Google Colab]


To count the number of pages in a PDF with PyMuPDF, you first need to open the PDF file. Check out this mini-guide: Open Your First PDF with PyMuPDF.

Top comments (0)