After exploring the PDF standard and a bit of hacking, I found that the following function will add a single PageLabels entry that creates page lables starting from offset (i.e. the first page will be labelled the offset, the second page, offset+1, etc.).

# output_pdf is an instance of PdfFileWriter().
# offset is the desired page offset.
def add_pagelabels(output_pdf, offset):
    number_type = PDF.DictionaryObject()
    number_type.update({PDF.NameObject("/S"):PDF.NameObject("/D")})
    number_type.update({PDF.NameObject("/St"):PDF.NumberObject(offset)})

    nums_array = PDF.ArrayObject()
    nums_array.append(PDF.NumberObject(0)) # physical page index
    nums_array.append(number_type)

    page_numbers = PDF.DictionaryObject()
    page_numbers.update({PDF.NameObject("/Nums"):nums_array})

    page_labels = PDF.DictionaryObject()
    page_labels.update({PDF.NameObject("/PageLabels"): page_numbers})

    root_obj = output_pdf._root_object
    root_obj.update(page_labels)

Additional page label entries can be created (i.e. with different offsets or different numbering styles).

Note that the first PDF page has an index of 0.

# Use PyPDF to manipulate pages
from PyPDF4 import PdfFileWriter, PdfFileReader 

# To manipulate the PDF dictionary
import PyPDF4.pdf as PDF

def pdf_pagelabels_roman():
    number_type = PDF.DictionaryObject()
    number_type.update({PDF.NameObject("/S"):PDF.NameObject("/r")})
    return number_type

def pdf_pagelabels_decimal():
    number_type = PDF.DictionaryObject()
    number_type.update({PDF.NameObject("/S"):PDF.NameObject("/D")})
    return number_type

def pdf_pagelabels_decimal_with_offset(offset):
    number_type = pdf_pagelabels_decimal()
    number_type.update({PDF.NameObject("/St"):PDF.NumberObject(offset)})
    return number_type

...
    nums_array = PDF.ArrayObject()
    # Each entry consists of an index followed by a page label...
    nums_array.append(PDF.NumberObject(0))  # Page 0:
    nums_array.append(pdf_pagelabels_roman()) # Roman numerals

    # Each entry consists of an index followed by a page label...
    nums_array.append(PDF.NumberObject(1)) # Page 1 -- 10:
    nums_array.append(pdf_pagelabels_decimal_with_offset(first_offset)) # Decimal numbers, with Offset

    # Each entry consists of an index followed by a page label...
    nums_array.append(PDF.NumberObject(10)) # Page 11 --> :
    nums_array.append(pdf_pagelabels_decimal_with_offset(second_offset))


    page_numbers = PDF.DictionaryObject()
    page_numbers.update({PDF.NameObject("/Nums"):nums_array})

    page_labels = PDF.DictionaryObject()
    page_labels.update({PDF.NameObject("/PageLabels"): page_numbers})

    root_obj = output._root_object
    root_obj.update(page_labels)
Answer from KevinM on Stack Overflow
🌐
PyPI
pypi.org › project › PyPDF4
PyPDF4 · PyPI
August 7, 2018 - Details for the file PyPDF4-1.27.0.tar.gz.
Help
The Python Package Index (PyPI) is a repository of software for the Python programming language.
Sponsors
The Python Package Index (PyPI) is a repository of software for the Python programming language.
Register
The Python Package Index (PyPI) is a repository of software for the Python programming language.
Log in
The Python Package Index (PyPI) is a repository of software for the Python programming language.
🌐
GitHub
github.com › claird › PyPDF4
GitHub - claird/PyPDF4: A utility to read and write PDFs with Python · GitHub
August 7, 2018 - PyPDF4 is a pure-python PDF library capable of splitting, merging together, cropping, and transforming the pages of PDF files. It can also add custom data, viewing options, and passwords to PDF files.
Starred by 337 users
Forked by 60 users
Languages   Python
Discussions

pdf generation - Python/PyPDF4: How do I specify the /PageLabels in the created PDF? - Stack Overflow
I am using PyPDF4 to create an offline-readable version of the journal "Nature". I use PyPDF4 PdfFileReader to read the individual article PDFs and PdfFileWriter to create a single, merged ouput. ... More on stackoverflow.com
🌐 stackoverflow.com
Installing PyPDF4 on Python3.9 on a MacBo… - Apple Community
I have the latest Python 3.9 with pip3 and ran "pip install ."PyPDF4-1.27.0.tar.gz" from the terminal More on discussions.apple.com
🌐 discussions.apple.com
Newest 'pypdf4' Questions - Stack Overflow
Stack Overflow | The World’s Largest Online Community for Developers More on stackoverflow.com
🌐 stackoverflow.com
PyPDF2 and PyPDF4 fails to extract text from the PDF - Python - The freeCodeCamp Forum
import PyPDF4 as p2 pdffile = open("XXXX.pdf","rb") pdfread=p2.PdfFileReader(pdffile) print(pdfread.getNumPages()) pageinfo=pdfread.getPage(0) print(pageinfo.extractText()) While running the above the 4th line of code successfully returns the correct value i.e no. of pages in the PDF, however, ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
September 11, 2021
🌐
Reddit
reddit.com › r/learnpython › pypdf vs pypdf2 vs pypdf3 vs pypdf4 vs others
r/learnpython on Reddit: PyPDF vs PyPDF2 vs PyPDF3 vs PyPDF4 vs others
February 20, 2023 -

Initially I just googled for a way to get the number of pages in a pdf file. First result was PyPDF2 so I just used that.

After a while I got an error from a file, so I started looking around and realized that there are 4 different forks of this library!

What is going on here? Why are there so many forks?

In other news, later on I will be scraping some text from some pdf files. Which library would you recommend? I won't be needing OCR, the text is already in the files.

Thanks!

🌐
PyPDF
pypdf.readthedocs.io › en › 4.0.0
Welcome to pypdf — pypdf 4.0.0 documentation
pypdf is a free and open source pure-python PDF library capable of splitting, merging, cropping, and transforming the pages of PDF files. It can also add custom data, viewing options, and passwords to PDF files. pypdf can retrieve text and metadata from PDFs as well · See pdfly for a CLI ...
Top answer
1 of 1
4

After exploring the PDF standard and a bit of hacking, I found that the following function will add a single PageLabels entry that creates page lables starting from offset (i.e. the first page will be labelled the offset, the second page, offset+1, etc.).

# output_pdf is an instance of PdfFileWriter().
# offset is the desired page offset.
def add_pagelabels(output_pdf, offset):
    number_type = PDF.DictionaryObject()
    number_type.update({PDF.NameObject("/S"):PDF.NameObject("/D")})
    number_type.update({PDF.NameObject("/St"):PDF.NumberObject(offset)})

    nums_array = PDF.ArrayObject()
    nums_array.append(PDF.NumberObject(0)) # physical page index
    nums_array.append(number_type)

    page_numbers = PDF.DictionaryObject()
    page_numbers.update({PDF.NameObject("/Nums"):nums_array})

    page_labels = PDF.DictionaryObject()
    page_labels.update({PDF.NameObject("/PageLabels"): page_numbers})

    root_obj = output_pdf._root_object
    root_obj.update(page_labels)

Additional page label entries can be created (i.e. with different offsets or different numbering styles).

Note that the first PDF page has an index of 0.

# Use PyPDF to manipulate pages
from PyPDF4 import PdfFileWriter, PdfFileReader 

# To manipulate the PDF dictionary
import PyPDF4.pdf as PDF

def pdf_pagelabels_roman():
    number_type = PDF.DictionaryObject()
    number_type.update({PDF.NameObject("/S"):PDF.NameObject("/r")})
    return number_type

def pdf_pagelabels_decimal():
    number_type = PDF.DictionaryObject()
    number_type.update({PDF.NameObject("/S"):PDF.NameObject("/D")})
    return number_type

def pdf_pagelabels_decimal_with_offset(offset):
    number_type = pdf_pagelabels_decimal()
    number_type.update({PDF.NameObject("/St"):PDF.NumberObject(offset)})
    return number_type

...
    nums_array = PDF.ArrayObject()
    # Each entry consists of an index followed by a page label...
    nums_array.append(PDF.NumberObject(0))  # Page 0:
    nums_array.append(pdf_pagelabels_roman()) # Roman numerals

    # Each entry consists of an index followed by a page label...
    nums_array.append(PDF.NumberObject(1)) # Page 1 -- 10:
    nums_array.append(pdf_pagelabels_decimal_with_offset(first_offset)) # Decimal numbers, with Offset

    # Each entry consists of an index followed by a page label...
    nums_array.append(PDF.NumberObject(10)) # Page 11 --> :
    nums_array.append(pdf_pagelabels_decimal_with_offset(second_offset))


    page_numbers = PDF.DictionaryObject()
    page_numbers.update({PDF.NameObject("/Nums"):nums_array})

    page_labels = PDF.DictionaryObject()
    page_labels.update({PDF.NameObject("/PageLabels"): page_numbers})

    root_obj = output._root_object
    root_obj.update(page_labels)
🌐
Medium
martinthoma.medium.com › pypdf-the-2022-review-8925dea750d9
pypdf: The 2022 Review. Better text extraction, encryption, and… | by Martin Thoma | Medium
December 25, 2022 - The library has quite a history. It started with pyPdf (2005–2010), continued with PyPDF2 (2011–2016), and had PyPDF3 and PyPDF4 (2018–2022).
Find elsewhere
🌐
piwheels
piwheels.org › project › pypdf4
piwheels - PyPDF4
The piwheels project page for PyPDF4: PDF toolkit
🌐
PyPDF
pypdf.readthedocs.io › en › latest › meta › comparisons.html
pypdf vs X — pypdf 6.14.2 documentation
As pypdf is free software, there were attempts to fork it and continue the development. PyPDF3 was first released in 2018 and still receives updates. PyPDF4 has only one release from 2018.
🌐
Safety
getsafety.com › packages › pypi › pypdf4
PyPDF4 (PyPI) — Safety Package & Vulnerability Database
Known vulnerabilities and security issues detected in the extension's dependencies and code · No vulnerabilities found for this package
🌐
GitHub
github.com › claird › PyPDF4 › blob › master › pypdf › pdf.py
PyPDF4/pypdf/pdf.py at master · claird/PyPDF4
A utility to read and write PDFs with Python. Contribute to claird/PyPDF4 development by creating an account on GitHub.
Author   claird
🌐
Apple Community
discussions.apple.com › thread › 252400914
Installing PyPDF4 on Python3.9 on a MacBo… - Apple Community
I have the latest Python 3.9 with pip3 and ran "pip install ."PyPDF4-1.27.0.tar.gz" from the terminal
🌐
Snyk
snyk.io › snyk vulnerability database › pip
pypdf4 | Snyk
Further analysis of the maintenance status of PyPDF4 based on released PyPI versions cadence, the repository activity, and other data points determined that its maintenance is Inactive.
🌐
PyPI
pypi.org › project › pypdf
pypdf · PyPI
A pure-python PDF library capable of splitting, merging, cropping, and transforming PDF files
      » pip install pypdf
    
Published   Jun 23, 2026
Version   6.14.2
🌐
freeCodeCamp
forum.freecodecamp.org › python
PyPDF2 and PyPDF4 fails to extract text from the PDF - Python - The freeCodeCamp Forum
September 11, 2021 - import PyPDF4 as p2 pdffile = open("XXXX.pdf","rb") pdfread=p2.PdfFileReader(pdffile) print(pdfread.getNumPages()) pageinfo=pdfread.getPage(0) print(pageinfo.extractText()) While running the above the 4th line of code successfully returns the correct value i.e no. of pages in the PDF, however, the 6th line (PDF extraction) gives a one page long blank data.
🌐
PyPDF
pypdf.readthedocs.io
Welcome to pypdf — pypdf 6.14.2 documentation
pypdf is a free and open source pure-python PDF library capable of splitting, merging, cropping, and transforming the pages of PDF files. It can also add custom data, viewing options, and passwords to PDF files. pypdf can retrieve text and metadata from PDFs as well · See pdfly for a CLI ...
🌐
Real Python
realpython.com › creating-modifying-pdf
Create and Modify PDF Files in Python – Real Python
January 19, 2025 - In this tutorial, you'll explore the different ways of creating and modifying PDF files in Python. You'll learn how to read and extract text, merge and concatenate files, crop and rotate pages, encrypt and decrypt files, and even create PDFs ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › working-with-pdf-files-in-python
Working with PDF files in Python - GeeksforGeeks
June 21, 2025 - Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
🌐
Nanonets
nanonets.com › blog › pypdf2-library-working-with-pdf-files-in-python
PYPDF2 Library: How Can You Work With PDF Files in Python?
July 11, 2025 - PyPDF4 is an advanced tool for working with PDF documents on the macOS, Windows, and Linux platforms.