如何使用 python 提取 PDF 表格及文本,并保存到 Excel
↓推荐关注↓
pdf 是一种便携式文档格式,由Adobe公司设计。因为不受平台限制,且方便保存和传输,所以pdf非常受欢迎。
目前市场上有很多pdf工具,大部分是阅读类,也有支持对pdf的修改、转换等功能,但这部分工具不少是收费的。
这次介绍一个开源python工具库-pdfplumber,可以方便地获取pdf的各种信息,包括文本、表格、图表、尺寸等。
pdfplumber在github上有英文官方文档,后面我们会捡重点讲解,先看下如何用pdfplumber提取pdf表格?
以NBA 2020-2021 常规赛数据作为范例,pdf表格如下:
第一步:使用pdfplumber提取表格文本
pdfplumber
# 读取pdf文件,保存为pdf实例pdf = pdfplumber.open(
"E:\\nba.pdf")
# 访问第二页first_page = pdf.pages[
1]
# 自动读取表格信息,返回列表table = first_page.extract_table()
table
输出:
第二步:整理成dataframe格式,保存为excel
pandas
as pd
# 将列表转为dftable_df = pd.DataFrame(table_2[
1:],columns=table_2[
0])
# 保存exceltable_df.to_excel(
'test.xlsx')
table_df
输出:
一个小小的脚本,不到十行代码,便将pdf表格提取并转化为dataframe格式,最终保存到excel。
有个初步认知后,接下来详细讲讲pdfplumber的安装、导入、api接口等信息。
pdfplumber简介
前面已经介绍过pdfplumber的用途,也用一个小案例展示了如何提取表格,我觉得对于pdfplumber只需要了解三点就可以。
1、它是一个纯python第三方库,适合python 3.x版本
2、它用来查看pdf各类信息,能有效提取文本、表格
3、它不支持修改或生成pdf,也不支持对pdf扫描件的处理
Github地址https://github.com/jsvine/pdfplumber
pdfplumber安装和导入
同其他python库一样,pdfplumber支持使用pip安装,在命令行输入:
如果遇到安装慢的问题,可以替换镜像源,会快很多。
pdfplumber安装后,用import导入即可使用:
pdfplumber
....
pdfplumber简单使用
pdfplumber中有两个基础类,PDF和Page。看字面意思能猜出,前者是处理整个文档,后者是处理页面。
「pdfplumber.PDF类」
.metadata | |
.pages | pdfplumber.Page 实例的列表,每一个实例代表pdf每一页的信息。 |
「pdfplumber.Page类」
这是pdfplumber的核心功能,对pdf的大部分操作都是基于这个类,包括提取文本、表格、尺寸等。
这里暂不一一列举它的属性和方法。
通过一个简单的案例,就可以明白它们的作用。示例pdf文档,共两页:
1. 读取pdf
pdfplumber
# 读取pdf文件,返回pdfplumber.PDF类的实例pdf = pdfplumber.open(
"e:\\nba2.pdf")
2. 获取该pdf文档的信息
输出:
这些是pdf的基础信息,包括作者、来源、日期等。
3. 总页数
4. 读取第一页的页宽、页高等信息
first_page = pdf.pages[
0]
# 查看页码print(
'页码:',first_page.page_number)
# 查看页宽print(
'页宽:'first_page.width)
# 查看页高print(
'页高:'first_page.height)
输出:
5. 读取第一页的文本
text = first_page.extract_text()
print(text)
输出:
读取第二页的表格
pandas
as pd
# 第二页pdfplumber.Page实例first_page = pdf.pages[
1]
# 自动读取表格信息,返回列表table = first_page.extract_tables()
# 将列表转为dftable_df = pd.DataFrame(table_2[
1:],columns=table_2[
0])
table_df
pdfplumber提取表格有很多的细节需要处理,这里给到的范例表格线框比较规范,所以能很简单的提取,但对于线框不完全(包含无线框)的表格,其效果就差了不少。
在实际项目所需处理的pdf文档中,线框完全及不完全的表格都比较多,为了能够理解pdfplumber实现表格抽取的原理和方法,我们需要去细究相关参数的设置。
正如案例所示,pdfplumber.Page对象的
.extract_table()
方法可以提取表格,返回从页面上最大的表中提取的文本,以列表列表的形式显示,结构为row -> cell。「表格抽取参数设置」
默认情况下,
extract_table
使用页面的垂直和水平线(或矩形边缘)作为单元格分隔符。该方法可以通过table_settings参数进行高度自定义。可能的设置及其默认值:{
"vertical_strategy":
"lines",
"horizontal_strategy":
"lines",
"explicit_vertical_lines": [],
"explicit_horizontal_lines": [],
"snap_tolerance":
3,
"join_tolerance":
3,
"edge_min_length":
3,
"min_words_vertical":
3,
"min_words_horizontal":
1,
"keep_blank_chars":
False,
"text_tolerance":
3,
"text_x_tolerance":
None,
"text_y_tolerance":
None,
"intersection_tolerance":
3,
"intersection_x_tolerance":
None,
"intersection_y_tolerance":
None,
}
pdfplumber支持对图表进行可视化调试,能输出图像,显示如何提取表。
pdfplumber的独特之处
python中有很多库可以处理pdf,比如PyPDF2、pdfminer等,那pdfplumber的优势在哪呢?
首先,pdfplumber能轻松访问有关PDF对象的所有详细信息,且用于提取文本和表格的方法高级可定制,使用者可根据表格的具体形式来调整参数。
最关键的是pdfplumber作者持续在维护该库,而同样受欢迎的PyPDF2已经不再维护了。
PS:后台回复关键字:pdf,获取文中案例及代码
- EOF -
觉得本文对你有帮助?请分享给更多人
推荐关注「Python开发者」,提升Python技能
点赞和在看就是最大的支持❤️
阅读原文 最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。