PyPDF4

raw JSON →
1.27.0 verified Fri May 01 auth: no python

PyPDF4 is a pure-Python PDF toolkit capable of splitting, merging, cropping, and transforming PDF files. Current version is 1.27.0. The library is in maintenance mode with slow updates.

pip install pypdf4
error ImportError: cannot import name 'PdfFileReader' from 'PyPDF4'
cause PyPDF4 is not installed or you have PyPDF2 installed instead.
fix
Run: pip install pypdf4
error TypeError: 'PdfFileReader' object is not callable
cause Using PyPDF4's PdfFileReader as a function instead of a class instantiation.
fix
Use: reader = PdfFileReader(stream)
error AttributeError: module 'PyPDF4' has no attribute 'PdfFileMerger'
cause PyPDF4 removed PdfFileMerger in favor of merging via PdfFileWriter.
fix
Use PdfFileWriter and its appendPagesFromReader method.
breaking PyPDF4 is not compatible with PyPDF2. Do not install both; they use similar imports and can conflict.
fix Uninstall PyPDF2 if you intend to use PyPDF4, or vice versa. Use only one library.
deprecated The numPages and getPage() methods are deprecated in favor of len(reader.pages) and reader.pages[index]. Future versions may remove the old API.
fix Transition to the modern API: pages = reader.pages; len(pages); pages[0].
gotcha The library uses a mix of camelCase and snake_case method names (e.g., numPages vs getPage).
fix Refer to the documentation for each method's exact name; future versions may standardize to snake_case.

Basic example: read a PDF and write its first page to a new file.

from PyPDF4 import PdfFileReader, PdfFileWriter

# Read a PDF
reader = PdfFileReader(open('example.pdf', 'rb'))
print(f"Number of pages: {reader.numPages}")

# Write a PDF
writer = PdfFileWriter()
writer.addPage(reader.getPage(0))
with open('output.pdf', 'wb') as f:
    writer.write(f)