pypdf

py-pdf活跃维护

纯 Python 实现的 PDF 处理库,能拆分、合并、裁剪和变换 PDF 页面,无需外部依赖即可在脚本里完成常见 PDF 操作。

A pure-python PDF library capable of splitting, merging, cropping, and transforming the pages of PDF files

7.0.0发布于 2026-11-06 12:09 UTC10,291star1,698fork同步于 2026-11-10 11:29 UTC

怎么上手

由 README 提炼,执行前请确认
pip install pypdf
硬件要求按 README 说明

README 未说明,纯 Python 实现,无 GPU 或显存要求

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

纯 Python 的 PDF 处理库,无需外部依赖即可拆分、合并、裁剪和转换 PDF 页面

纯 Python 实现的 PDF 处理库,能拆分、合并、裁剪和变换 PDF 页面,无需外部依赖即可在脚本里完成常见 PDF 操作。

核心能力

  • 用 PdfReader 逐页读取并提取文本与元数据
  • 拆分、合并、裁剪和变换 PDF 页面
  • 为 PDF 添加自定义数据、查看选项和密码
  • 支持 PDF 的加密与解密(AES 需额外依赖)
  • 读写和创建 PDF 注释

容易踩的坑

使用 AES 加密或解密需额外安装 pip install pypdf[crypto];3.1.0 及以上版本与旧版差异较大,升级需参考迁移指南;README 未提供命令行工具,CLI 需另装 pdfly

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本7.0.0
  1. 7.0.0Version 7.0.0, 2026-11-06最新正式版2026-11-06 · 2026-11-06 12:09 UTC

    What's new

    Security (SEC)

    • Make file identifiers non-deterministic (#4366) by @stefan6419846
    • Block not strictly increasing offsets in object streams (#4364) by @stefan6419846
    • Limit XFA form decoding work (#4363) by @stefan6419846
    • Apply image size limits more strictly (#4361) by @stefan6419846

    Deprecations (DEP)

    • Cleanup deprecations for the next major release (#4306) by @stefan6419846
    • Remove support for cryptography<43 (#4304) by @stefan6419846
    • Drop support for Python 3.9 (#4292) by @stefan6419846

    New Features (ENH)

    • Allow setting border color for rectangle annotations (#4297) by @Ak4shh

    Performance Improvements (PI)

    • Speed up LZW decoding for clear table markers (#4354) by @stefan6419846
    • Build right-to-left text runs with a single join (#4344) by @kholdrex
    • Read every page label number tree once for all pages (#4321) by @kholdrex
    • Decode the PNG Up predictor a row at a time (#4312) by @MartinThoma

    Bug Fixes (BUG)

    • Fix cloning of objects that require constructor arguments (#4262) by @Ak4shh
    • Serialize XMP packets without an XML declaration (#4296) by @Ak4shh

    Maintenance (MAINT)

    • Generate Symbol and ZapfDingbats encodings from Adobe sources (#4333) by @plenaerts

    Full Changelog

  2. 6.21.4Version 6.21.4, 2026-10-282026-10-28 · 2026-10-28 17:00 UTC
    查看更新说明

    What's new

    Security (SEC)

    • Limit the number of copied page attributes during flattening (#4290) by @stefan6419846
    • Detect EOF when reading standard xref table trailer (#4289) by @stefan6419846
    • Fix loop detection when cloning DictionaryObject (#4288) by @stefan6419846

    Documentation (DOC)

    • Exclude performance issues from security reports (#4280) by @stefan6419846

    Full Changelog

  3. 6.21.3Version 6.21.3, 2026-10-272026-10-27 · 2026-10-27 12:14 UTC
    查看更新说明

    What's new

    Security (SEC)

    • Use the validated page count after decrypting a document (#4270) by @stefan6419846
    • Limit total number of cross-reference stream entries (#4269) by @stefan6419846
    • Avoid excessive error messages (#4265) by @stefan6419846

    Performance Improvements (PI)

    • Read flat page label number trees once for all pages (#4251) by @kholdrex
    • Collect the named destinations once when resolving links (#4249) by @MartinThoma
    • Avoid scanning all objects in get_outline_root (#4245) by @kholdrex

    Bug Fixes (BUG)

    • Resolve indirect widths in CID font /W arrays (#4267) by @tim-hilde

    Documentation (DOC)

    • Block incremental writes of encrypted documents (#4246) by @metsw24-max

    Developer Experience (DEV)

    • Update to PyPy 3.12 and pinned requirements (#4244) by @stefan6419846

    Maintenance (MAINT)

    • Revert: Reject unsupported key lengths in the /Encrypt dictionary by @stefan6419846

    Full Changelog

  4. 6.21.2Version 6.21.2, 2026-10-212026-10-21 · 2026-10-21 16:44 UTC
    查看更新说明

    What's new

    Security (SEC)

    • Avoid repeated full file loads (#4240) by @stefan6419846
    • Limit size of standard xref table (#4239) by @stefan6419846

    Bug Fixes (BUG)

    • Decode UniGB-UTF16 CMaps as UTF-16BE (#4231) by @4444wjt
    • Keep 8-bit DeviceRGB image mode (#4230) by @DGrutt
    • Preserve all annotation quadrilaterals when merging pages (#4223) by @repzakske

    Robustness (ROB)

    • Multiple fixes

    Full Changelog

  5. 6.21.1Version 6.21.1, 2026-10-172026-10-17 · 2026-10-17 10:29 UTC
    查看更新说明

    What's new

    Security (SEC)

    • Only open /DCTDecode image data with the Pillow JPEG plugin (#4222) by @metsw24-max

    Full Changelog

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    PyPI version Python Support GitHub last commit codecov

    pypdf

    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 application that uses pypdf to interact with PDFs.

    Installation

    Install pypdf using pip:

    pip install pypdf
    

    For using pypdf with AES encryption or decryption, install extra dependencies:

    pip install pypdf[crypto]
    

    NOTE: pypdf 3.1.0 and above include significant improvements compared to previous versions. Please refer to the migration guide for more information.

    Usage

    from pypdf import PdfReader
    
    reader = PdfReader("example.pdf")
    number_of_pages = len(reader.pages)
    page = reader.pages[0]
    text = page.extract_text()
    

    pypdf can do a lot more, e.g. splitting, merging, reading and creating annotations, decrypting and encrypting. Check out the documentation for additional usage examples!

    For questions and answers, visit StackOverflow (tagged with pypdf).

    Contributions

    Maintaining pypdf is a collaborative effort. You can support the project by writing documentation, helping to narrow down issues, and submitting code. See the CONTRIBUTING.md file for more information.

    Q&A

    The experience pypdf users have covers the whole range from beginner to expert. You can contribute to the pypdf community by answering questions on StackOverflow, helping in discussions, and asking users who report issues for MCVE's (Code + example PDF!).

    Issues

    A good bug ticket includes a MCVE - a minimal complete verifiable example. For pypdf, this means that you must upload a PDF that causes the bug to occur as well as the code you're executing with all of the output. Use print(pypdf.__version__) to tell us which version you're using.

    Code

    All code contributions are welcome, but smaller ones have a better chance to get included in a timely manner. Adding unit tests for new features or test cases for bugs you've fixed help us to ensure that the Pull Request (PR) is fine.

    pypdf includes a test suite which can be executed with pytest:

    $ pytest
    ===================== test session starts =====================
    platform linux -- Python 3.6.15, pytest-7.0.1, pluggy-1.0.0
    rootdir: /home/moose/GitHub/Martin/pypdf
    plugins: cov-3.0.0
    collected 233 items
    
    tests/test_basic_features.py ..                         [  0%]
    tests/test_constants.py .                               [  1%]
    tests/test_filters.py .................x.....           [ 11%]
    tests/test_generic.py ................................. [ 25%]
    .............                                           [ 30%]
    tests/test_javascript.py ..                             [ 31%]
    tests/test_merger.py .                                  [ 32%]
    tests/test_page.py .........................            [ 42%]
    tests/test_pagerange.py ................                [ 49%]
    tests/test_papersizes.py ..................             [ 57%]
    tests/test_reader.py .................................. [ 72%]
    ...............                                         [ 78%]
    tests/test_utils.py ....................                [ 87%]
    tests/test_workflows.py ..........                      [ 91%]
    tests/test_writer.py .................                  [ 98%]
    tests/test_xmp.py ...                                   [100%]
    
    ========== 232 passed, 1 xfailed, 1 warning in 4.52s ==========