py-pdf/pypdf
GitHub: py-pdf/pypdf
pypdf 是一个纯 Python 的开源 PDF 处理库,提供页面拆分、合并、裁剪、文本提取、加密解密等功能,帮助开发者在代码中高效操作 PDF 文件。
Stars: 10067 | Forks: 1591
[](https://badge.fury.io/py/pypdf)
[](https://pypi.org/project/pypdf/)
[](https://pypdf.readthedocs.io/en/stable/)
[](https://github.com/py-pdf/pypdf)
[](https://codecov.io/gh/py-pdf/pypdf)
# pypdf
pypdf 是一个免费且开源的纯 Python PDF 库,能够拆分、
[合并](https://pypdf.readthedocs.io/en/stable/user/merging-pdfs.html)、
[裁剪和转换](https://pypdf.readthedocs.io/en/stable/user/cropping-and-transforming.html)
PDF 文件的页面。它还可以向 PDF 文件添加自定义数据、查看选项和
[密码](https://pypdf.readthedocs.io/en/stable/user/encryption-decryption.html)。pypdf 也可以从 PDF 中
[提取文本](https://pypdf.readthedocs.io/en/stable/user/extract-text.html)
和
[元数据](https://pypdf.readthedocs.io/en/stable/user/metadata.html)。
查看 [pdfly](https://github.com/py-pdf/pdfly),这是一个使用 pypdf 与 PDF 交互的 CLI 应用程序。
## 安装
使用 pip 安装 pypdf:
```
pip install pypdf
```
要使用 pypdf 进行 AES 加密或解密,请安装额外的依赖项:
```
pip install pypdf[crypto]
```
## 用法
```
from pypdf import PdfReader
reader = PdfReader("example.pdf")
number_of_pages = len(reader.pages)
page = reader.pages[0]
text = page.extract_text()
```
pypdf 能做的远不止这些,例如拆分、合并、读取和创建注释、解密和加密。请查看
[文档](https://pypdf.readthedocs.io/en/stable/) 获取更多用法示例!
如需提问和解答,请访问
[StackOverflow](https://stackoverflow.com/questions/tagged/pypdf)
(标记为 [pypdf](https://stackoverflow.com/questions/tagged/pypdf))。
### 问答
pypdf 用户的经验涵盖了从初学者到专家的各个阶段。您可以通过在
[StackOverflow](https://stackoverflow.com/questions/tagged/pypdf) 上回答问题、
在 [讨论](https://github.com/py-pdf/pypdf/discussions) 中提供帮助,
以及要求报告问题的用户提供 [MCVE](https://stackoverflow.com/help/minimal-reproducible-example)(代码 + 示例 PDF!)来为 pypdf 社区做出贡献。
### 问题
一个优秀的 bug 报告应包含一个 MCVE —— 最小完整可验证示例。
对于 pypdf,这意味着您必须上传一个会导致该 bug 出现的 PDF 文件,
以及您正在执行的代码和所有输出。使用
`print(pypdf.__version__)` 告诉我们您使用的是哪个版本。
### 代码
欢迎所有代码贡献,但较小的贡献有更大的机会被及时合并。为新功能添加单元测试,或为您修复的 bug 添加测试用例,有助于我们确保 Pull Request (PR) 没有问题。
pypdf 包含一个测试套件,可以使用 `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 ==========
```
标签:PDF, Python, 开发工具库, 文件处理, 文档解析, 无后门, 逆向工具