python PyPDF2处理PDF文件
- 安装PyPDF2
pip install PyPDF2
- 官方文档:
- 导入模块
import PyPDF2
1. 读取PDF中的内容
# author:mlnt
# createdate:2022/8/16
import PyPDF2 # 导入PyPDF2模块
# 官方文档:https://pypi.org/project/PyPDF2/
# 1.打开PDF文件
pdf = open(file='test.pdf', mode='rb') # 以二进制方式打开
# 2.获取PDF文件的页数
# 打开PDF文件成功后,可使用PdfFileReader()方法读取PDF内容
pdf_reader = PyPDF2.PdfFileReader(pdf) # 读取PDF内容
print(f'PDF页数为:{
pdf_reader.numPages}')
print(f'PDF页数为:{
len(pdf_reader.pages)}')
# 3.读取PDF页面内容
"""
- 使用PdfFileReader()方法读取PDF文件后,可使用getPage(n)(或pages[n])获取第n页的PDF内容
- PDF页面从第0页开始计算
- 页面内容被读入后,可使用extractText()取得该页的字符串内容
"""
for i in range(pdf_reader.numPages):
pageObj = pdf_reader.getPage(i) # 读取第i页内容
# pageObj = pdf_reader.pages[i] # 读取第i页内容
page_content = pageObj.extractText() # 提取页面内容
print(page_content)
test.pdf:

读取效果:

2. PDF简单加密与解密
# author:mlnt
# createdate:2022/8/16
import PyPDF2
from PyPDF2 import PdfReader,</

本文介绍了Python的PyPDF2库,用于处理PDF文件,包括读取内容、加密解密、合并文件、处理页面重叠和添加水印等功能。通过示例代码详细展示了如何使用PyPDF2进行各种操作。

1万+

被折叠的 条评论
为什么被折叠?



