{"id":5701,"library":"pypdf2","title":"PyPDF2 (DEPRECATED: migrate to PyPDF)","description":"PyPDF2 is a pure-Python library designed for PDF file manipulation, offering capabilities like splitting, merging, cropping, and transforming PDF pages. The `pypdf2` package on PyPI, with its final major version 3.0.1, is now officially deprecated. It functions as a compatibility wrapper, internally using the API of `pypdf` version 3.0.1. All active development, new features, and security updates are happening under the `pypdf` project (currently at version 6.x.x), which is the recommended library for all new and ongoing PDF processing tasks in Python.","status":"deprecated","version":"3.0.1","language":"en","source_language":"en","source_url":"https://github.com/py-pdf/PyPDF2","tags":["pdf","document processing","deprecated","file manipulation"],"install":[{"cmd":"pip install pypdf2","lang":"bash","label":"Install deprecated PyPDF2 (wraps pypdf 3.0.1)"},{"cmd":"pip install pypdf","lang":"bash","label":"RECOMMENDED: Install active PyPDF library"},{"cmd":"pip install pypdf[crypto]","lang":"bash","label":"Install active PyPDF with AES encryption support"}],"dependencies":[{"reason":"Required for AES encryption and decryption if the 'crypto' extra is installed with pypdf.","package":"PyCryptodome","optional":true}],"imports":[{"note":"For `pypdf2 >= 3.0.0` (which wraps `pypdf`) and all `pypdf` versions, this is the correct modern import. Older `PyPDF2` versions (< 3.0.0) used `import PyPDF2` and accessed classes like `PyPDF2.PdfFileReader`.","wrong":"import PyPDF2","symbol":"PdfReader, PdfWriter","correct":"from pypdf import PdfReader, PdfWriter"}],"quickstart":{"code":"from pypdf import PdfReader, PdfWriter\nimport os\n\n# Create a dummy PDF for demonstration if it doesn't exist\ndummy_pdf_path = \"example.pdf\"\nif not os.path.exists(dummy_pdf_path):\n    writer = PdfWriter()\n    writer.add_blank_page(width=72, height=72)\n    writer.add_blank_page(width=72, height=72)\n    with open(dummy_pdf_path, \"wb\") as f:\n        writer.write(f)\n\n# --- Example: Read, extract text, and merge pages using pypdf (successor to PyPDF2) ---\n\n# Create a PdfReader object\nreader = PdfReader(dummy_pdf_path)\n\n# Get number of pages\nnum_pages = len(reader.pages)\nprint(f\"Number of pages: {num_pages}\")\n\n# Extract text from the first page\nfirst_page = reader.pages[0]\ntext = first_page.extract_text()\nprint(f\"Text from first page: '{text.strip() if text else 'No text'}'\")\n\n# Create a PdfWriter object for merging\nwriter = PdfWriter()\n\n# Add all pages from the reader to the writer\nfor page in reader.pages:\n    writer.add_page(page)\n\n# Add a blank page\nwriter.add_blank_page(width=72, height=72)\n\n# Write the output PDF to a file\noutput_pdf_path = \"merged_output.pdf\"\nwith open(output_pdf_path, \"wb\") as fp:\n    writer.write(fp)\n\nprint(f\"Successfully created {output_pdf_path} with {len(writer.pages)} pages.\")\n\n# Clean up dummy file\nos.remove(dummy_pdf_path)\nos.remove(output_pdf_path)","lang":"python","description":"This quickstart demonstrates basic PDF operations (reading, extracting text, merging, and adding pages) using `pypdf`, the actively maintained successor to `PyPDF2`. It creates a dummy PDF if one doesn't exist for the example to run."},"warnings":[{"fix":"Uninstall `pypdf2` (`pip uninstall pypdf2`), then install `pypdf` (`pip install pypdf`). Update import statements and class names in your code.","message":"The `PyPDF2` project has been officially renamed to `pypdf` and is now actively maintained under that name. The `pypdf2` PyPI package (version 3.0.1) is deprecated and acts as a wrapper around an older version of `pypdf` (specifically, `pypdf` 3.0.1). Users are strongly advised to migrate to `pypdf` for ongoing support, new features, and critical security updates.","severity":"breaking","affected_versions":"All `pypdf2` versions"},{"fix":"Update imports to `from pypdf import PdfReader, PdfWriter` and rename class instances (e.g., `PdfFileReader` to `PdfReader`, `PdfFileWriter` to `PdfWriter`).","message":"Prior to `PyPDF2` version 3.0.0 (which became the `pypdf` 3.0.1 wrapper), the API involved `import PyPDF2` and class names like `PyPDF2.PdfFileReader` and `PyPDF2.PdfFileWriter`. The modern `pypdf` API (and the `pypdf2 >= 3.0.0` wrapper) uses `from pypdf import PdfReader, PdfWriter` and respective class names.","severity":"breaking","affected_versions":"PyPDF2 < 3.0.0"},{"fix":"Migrate to the latest `pypdf` release (`pip install --upgrade pypdf`) to benefit from crucial security fixes and performance enhancements.","message":"Older versions of `PyPDF2` (pre-3.0.0, i.e., those that are not the `pypdf` 3.0.1 wrapper) contain known performance issues and critical security vulnerabilities, including infinite loop exploits and denial-of-service vectors. Even `pypdf2` 3.0.1, while wrapping `pypdf` 3.0.1, is significantly behind the latest `pypdf` (currently 6.x.x), which has received numerous security patches and performance improvements. Continuing to use `pypdf2` is not recommended for security-sensitive applications.","severity":"gotcha","affected_versions":"All `PyPDF2` versions; `pypdf2` 3.0.1 (compared to latest `pypdf`)"},{"fix":"Always use `pip install pypdf` and refer to the official `pypdf` documentation to ensure you are using the correct and most up-to-date library.","message":"The history of Python PDF libraries is complex, with several forks and renames including `pyPdf`, `PyPDF2`, `PyPDF3`, and `PyPDF4`. This can lead to significant confusion regarding which library is current and actively maintained. `pypdf` (the successor to `PyPDF2`) is the currently recommended and actively developed library.","severity":"gotcha","affected_versions":"All users of Python PDF libraries"},{"fix":"Review your code for `replace_contents` usage. Consult the `pypdf` documentation for alternative approaches or ensure you are using it only as intended by the library maintainers.","message":"The method `PageObject.replace_contents` was documented as potentially problematic and its usage on `PdfReader` objects was specifically advised against in `pypdf` 6.8.0. Incorrect usage can lead to unintended side effects or corrupted PDF files.","severity":"deprecated","affected_versions":"`pypdf` (and thus `pypdf2` wrapper) versions 6.8.0 and higher where `replace_contents` is used in a potentially problematic way."}],"env_vars":null,"last_verified":"2026-03-29T00:00:00.000Z","next_check":"2026-06-27T00:00:00.000Z"}