一文教会你用Python读取PDF文件
作者:梦想橡皮擦 时间:2021-03-23 00:24:55
实战场景
Python 工程师在日常的工作中,经常会碰到解析和处理PDF文件的情况,实战中需求主要分为如下情况:
提取 PDF 中的文字
将 PDF 中每页转换为图片
word 转换为PDF
PDF生成,编辑,导入导出
PDF在线渲染
除了最后一项需要前端配合以外,其余内容都可以直接在 python 端进行实现。
本次实战选择 pdfplumber
库进行学习,可以提前安装该库,不过有一点需要注意,该库主要用于读取 PDF 进行操作,写入和编辑无法实现,即本文学习一款专注于 PDF 内容提取的库。
> pip install pdfplumber -i https://pypi.tuna.tsinghua.edu.cn/simple
pdfplumber
库具备如下特点:
可以访问PDF对象中的任意元素详细信息;
可以提取文本和表格,而且用法简单;
集成了可视化调试。
Python PDF 实战编码
下面可以编写 PDF 操作的基础代码。
import pdfplumber
with pdfplumber.open('./dddd.pdf') as pdf:
for page in pdf.pages:
print(page.extract_text())
# 每页打印一分页分隔
print('---------- 分页分隔 ----------')
导入 pdfplumber
模块之后,使用 pdfplumber.open('./dddd.pdf')
打开本地 pdf 文件,然后通过 pdf.pages
遍历所有页,在通过页对象的 .extract_text()
方法,提取文本信息。
pdfplumber.open() 方法的签名如下所示:
pdfplumber.open("文件名", password = "密码", laparams = { "line_overlap": 0.7 })
其中各参数描述如下:
file_name
:文件名,必选参数;password
:PDF的密码;laparams
:布局参数。
除此之外,如果希望读取 PDF,还可以使用 load()
方法,该方法也会返回 pdfplumber.PDF
类的实例。
pdfplumber.PDF
对象实例,主要有两个重要属性:
.metadata
:从PDF的Info中获取元数据键 /值对字典。 通常包括“ CreationDate”,“ ModDate”,“ Producer”等;.pages
:包含pdfplumber.Page
实例的列表,每一个实例代表PDF每一页的信息。
上文提及的 pdfplumber.Page
实例是 pdfplumber
的核心,后续对 PDF 的操作大量围绕该类的属性和方法实施,其重要属性如下所示:
page_number
:页码顺序,第一页的序号是 1;witdh
:宽度;height
:高度;.objects/.chars/.lines/.rects/.curves/.figures/.images
:获取PDF页中的重要数据。
核心方法如下所示:
extract_text()
:提取页中的文本;extract_words()
:提取所有单词及其相关信息;extract_tables()
: 提取页面的表格。
extract_text()
呈现结果
extract_words()
呈现结果
extract_tables()
呈现效果,由于 PDF 中无表格,所有每页得到的都是空!
补充
当然Python除了读取PDF文件,还有一些其他功能,例如加密PDF,旋转和叠加页面等,下面是实现的示例代码
旋转和叠加页面
import PyPDF2
from PyPDF2.pdf import PageObject
# 创建一个读PDF文件的Reader对象
reader = PyPDF2.PdfFileReader('resources/XGBoost.pdf')
# 创建一个写PDF文件的Writer对象
writer = PyPDF2.PdfFileWriter()
# 对PDF文件所有页进行循环遍历
for page_num in range(reader.numPages):
# 获取指定页码的Page对象
current_page = reader.getPage(page_num) # type: PageObject
if page_num % 2 == 0:
# 奇数页顺时针旋转90度
current_page.rotateClockwise(90)
else:
# 偶数页反时针旋转90度
current_page.rotateCounterClockwise(90)
writer.addPage(current_page)
# 最后添加一个空白页并旋转90度
page = writer.addBlankPage() # type: PageObject
page.rotateClockwise(90)
# 通过Writer对象的write方法将PDF写入文件
with open('resources/XGBoost-modified.pdf', 'wb') as file:
writer.write(file)
加密PDF文件
import PyPDF2
reader = PyPDF2.PdfFileReader('resources/XGBoost.pdf')
writer = PyPDF2.PdfFileWriter()
for page_num in range(reader.numPages):
writer.addPage(reader.getPage(page_num))
# 通过encrypt方法加密PDF文件,方法的参数就是rre
#设置的密码
writer.encrypt('foobared')
with open('resources/XGBoost-encrypted.pdf', 'wb') as file:
writer.write(file)
批量添加水印
import PyPDF2
from PyPDF2.pdf import PageObject
reader1 = PyPDF2.PdfFileReader('resources/XGBoost.pdf') reader2 = PyPDF2.PdfFileReader('resources/watermark.pdf')
writer = PyPDF2.PdfFileWriter()
# 获取水印页
watermark_page = reader2.getPage(0)
for page_num in range(reader1.numPages):
current_page = reader1.getPage(page_num) # type: PageObject current_page.mergePage(watermark_page)
# 将原始页和水印页进行合并
writer.addPage(current_page)
# 将PDF写入文件
with open('resources/XGBoost-watermarked.pdf', 'wb') as file:
writer.write(file)
来源:https://juejin.cn/post/7129692963273179149
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python利用matplotlib库绘制饼图的方法示例
![](https://img.aspxhome.com/file/2023/3/97263_0s.png)
Python 多线程爬取案例
![](https://img.aspxhome.com/file/2023/6/90546_0s.png)
详解python之多进程和进程池(Processing库)
![](https://img.aspxhome.com/file/2023/9/89299_0s.png)
Pandas中describe()函数的具体使用
![](https://img.aspxhome.com/file/2023/6/99076_0s.jpg)
Python可视化神器pyecharts绘制地理图表
![](https://img.aspxhome.com/file/2023/0/102840_0s.png)
MySQL数据库线程缓冲池详解
卸载VS2011 Developer Preview后Sql Server2008 R2建立数据库关系图报“找不到指定的模块”错误的解决方法
Jupyter Notebook添加代码自动补全功能的实现
![](https://img.aspxhome.com/file/2023/5/86435_0s.png)
Symfony2实现在controller中获取url的方法
PHP面向对象程序设计高级特性详解(接口,继承,抽象类,析构,克隆等)
详解ASP中断开记录集的使用方法
什么设计师应该学习编写代码[译]
![](https://img.aspxhome.com/file/UploadPic/20097/8/11-01_designers_code_leading_img-13s.jpg)
奇淫技巧之图片切割
深入理解Python中字典的键的使用
python可变对象,不可变对象详解
![](https://img.aspxhome.com/file/2023/5/76025_0s.png)
PHP 引用的概念
python matplotlib绘画十一种常见数据分析图
![](https://img.aspxhome.com/file/2023/0/65850_0s.png)
Python图像处理模块ndimage用法实例分析
![](https://img.aspxhome.com/file/2023/6/100816_0s.png)
Python标准库之typing的用法(类型标注)
![](https://img.aspxhome.com/file/2023/0/65100_0s.png)
Python批量解压&压缩文件夹的示例代码
![](https://img.aspxhome.com/file/2023/8/93948_0s.png)