liveBook · Manning

PdfReader

As explained in Learn AI-Assisted Python Programming, Second Edition

PdfReader is a PyPDF2 API class for reading and accessing the contents of an existing PDF document, replacing PdfFileReader in later versions.

PdfReader is the PyPDF2 class for reading an existing PDF. It replaces PdfFileReader in PyPDF2 3.0.0, so older merging scripts may need this name changed. section 9.4.2

Updating a PDF-merging script

A report-merging function may create a reader for each report, inspect its pages, and pass them to a writer. If the script calls PdfFileReader under PyPDF2 3.0.0, it raises DeprecationError and identifies PdfReader as the replacement. section 9.4.2 section 9.4.1

Traceback (most recent call last):
  File "merge_pdfs.py", line 34, in <module>
    add_covers('reports', 'covers', 'final')
  File "merge_pdfs.py", line 20, in add_covers
    report_reader = PyPDF2.PdfFileReader(report)     #1
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\PyPDF2\_reader.py", line 1974, in __init__
    deprecation_with_replacement("PdfFileReader", "PdfReader", "3.0.0")
  File "...\PyPDF2\_utils.py", line 369, in deprecation_with_replacement
    deprecation(DEPR_MSG_HAPPENED.format(old_name, removed_in, new_name))
  File "...\PyPDF2\_utils.py", line 351, in deprecation
    raise DeprecationError(msg)
PyPDF2.errors.DeprecationError: PdfFileReader is    #2
deprecated and was removed in PyPDF2 3.0.0. Use     #2
PdfReader instead.                                  #2

Trace the failure from the outdated constructor call to the recommended PdfReader replacement. section 9.4.2

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Defines constructor use and a deprecation traceback, but does not detail reader internals.
Examples
Uses report files, cover pages, page iteration, and a PdfFileReader-to-PdfReader migration error.
What it assumes
Assumes basic Python file handling and introduces PyPDF2 through a practical PDF-merging task.
What you can do after
Diagnose/debug with it by replacing PdfFileReader when PyPDF2 3.0.0 raises DeprecationError.
How it connects
Connects PDF reading with PyPDF2, PdfFileWriter, PDF pages, and PDF merging.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.