Python提取PDF内容的方法(文本、图像、线条等)
作者:查永春 时间:2023-07-03 13:16:59
1.安装PDFminer3k
使用pip 命令安装
pip install pdfminer3k
2.编写测试
你可以在这里获得官方参考:PDFMiner
如果你不喜欢看英文的官方文档,这里的翻译也许对你有帮助:中文PDFMiner文档
下面的程序,我拓展了官方给出的例子,你可以通过这个例子统计出来你的pdf文件一共包含哪些内容,比如文本框,曲线,图片等
#!/usr/bin/python
# -*- coding: utf-8 -*-
__author__ = 'yooongchun'
import sys
import importlib
importlib.reload(sys)
from pdfminer.pdfparser import PDFParser,PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import *
from pdfminer.pdfinterp import PDFTextExtractionNotAllowed
'''
解析pdf文件,获取文件中包含的各种对象
'''
# 解析pdf文件函数
def parse(pdf_path):
fp = open(pdf_path, 'rb') # 以二进制读模式打开
# 用文件对象来创建一个pdf文档分析器
parser = PDFParser(fp)
# 创建一个PDF文档
doc = PDFDocument()
# 连接分析器 与文档对象
parser.set_document(doc)
doc.set_parser(parser)
# 提供初始化密码
# 如果没有密码 就创建一个空的字符串
doc.initialize()
# 检测文档是否提供txt转换,不提供就忽略
if not doc.is_extractable:
raise PDFTextExtractionNotAllowed
else:
# 创建PDf 资源管理器 来管理共享资源
rsrcmgr = PDFResourceManager()
# 创建一个PDF设备对象
laparams = LAParams()
device = PDFPageAggregator(rsrcmgr, laparams=laparams)
# 创建一个PDF解释器对象
interpreter = PDFPageInterpreter(rsrcmgr, device)
# 用来计数页面,图片,曲线,figure,水平文本框等对象的数量
num_page, num_image, num_curve, num_figure, num_TextBoxHorizontal = 0, 0, 0, 0, 0
# 循环遍历列表,每次处理一个page的内容
for page in doc.get_pages(): # doc.get_pages() 获取page列表
num_page += 1 # 页面增一
interpreter.process_page(page)
# 接受该页面的LTPage对象
layout = device.get_result()
for x in layout:
if isinstance(x,LTImage): # 图片对象
num_image += 1
if isinstance(x,LTCurve): # 曲线对象
num_curve += 1
if isinstance(x,LTFigure): # figure对象
num_figure += 1
if isinstance(x, LTTextBoxHorizontal): # 获取文本内容
num_TextBoxHorizontal += 1 # 水平文本框对象增一
# 保存文本内容
with open(r'test.txt', 'a') as f:
results = x.get_text()
f.write(results + '\n')
print('对象数量:\n','页面数:%s\n'%num_page,'图片数:%s\n'%num_image,'曲线数:%s\n'%num_curve,'水平文本框:%s\n'
%num_TextBoxHorizontal)
if __name__ == '__main__':
pdf_path = r'C:\Users\fanyu\Desktop\pdf\test.pdf'
parse(pdf_path)
其实在上面的layout 对象中有更多的内容可提取,这个自己按需来写就好,然后对曲线,文本框等对象,都会有位置属性,可直接获取,自己debug 查看以下对象属性获取即可。
来源:https://blog.csdn.net/zyc121561/article/details/77877912
标签:Python,提取,PDF
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python数据分析:关键字提取方式
2022-03-08 09:23:37
VSCode使用ssh密钥免密远程登录服务器的方法
2022-03-13 22:42:13
![](https://img.aspxhome.com/file/2023/4/117524_0s.png)
pytorch实现线性拟合方式
2021-12-30 01:50:32
![](https://img.aspxhome.com/file/2023/7/112807_0s.jpg)
通过实例了解Python str()和repr()的区别
2022-06-01 21:37:36
python的环境conda简介
2022-12-24 18:40:30
菜鸟来看看:制作个人主页有诀窍
2007-10-05 09:03:00
PHP桥接模式Bridge Pattern的优点与实现过程
2023-05-25 06:53:44
Golang实现AES对称加密的过程详解
2024-01-31 06:44:22
Python实现将16进制字符串转化为ascii字符的方法分析
2023-11-17 12:26:25
python实现网站的模拟登录
2022-09-25 23:33:56
Python运行错误异常代码含义对照表
2023-11-14 09:20:09
PHP curl get post 请求的封装函数示例【get、post、put、delete等请求类型】
2023-05-25 01:24:18
零基础写python爬虫之使用urllib2组件抓取网页内容
2021-08-16 01:13:22
![](https://img.aspxhome.com/file/2023/1/127871_0s.png)
PHP中的Trait 特性及作用
2023-11-21 08:04:15
窥探mysql存储过程细节
2024-01-13 07:27:52
python中使用 xlwt 操作excel的常见方法与问题
2021-09-12 05:10:02
基于Python自制一个文件解压缩小工具
2021-10-15 14:28:00
良好体验,必须是全方位的
2009-09-24 13:32:00
![](https://img.aspxhome.com/file/UploadPic/20099/24/15-37s.png)
Python 实现中值滤波、均值滤波的方法
2022-05-09 08:58:38
![](https://img.aspxhome.com/file/2023/0/121100_0s.jpg)
探析Windows下将gvim8配置为Python IDE的方法
2021-02-02 06:28:30
![](https://img.aspxhome.com/file/2023/4/134684_0s.png)