python读取pdf表格_python 提取pdf文件中的信息

python 读取pdf文件有3个扩展包   pdfminer3k(python2中为pdfminer)、fitz和pymupdf

1.pdfminer3k

读取并获得pdf文档中的信息:

from pdfminer.pdfparser import PDFParser,PDFDocument

from pdfminer.pdfinterp import PDFResourceManager,PDFPageInterpreter,PDFTextExtractionNotAllowed

from pdfminer.converter import PDFPageAggregator

from pdfminer.layout import LTTextBoxHorizontal,LAParams,LTTextLineHorizontal,LTFigure,LTRect,LTLine,LTCurve

# 文件对象

pd_file = open("d.pdf", "rb")

# pdf文件解析对象

parser = PDFParser(pd_file)

# print(parser)

# pdf文档对象

document = PDFDocument()

parser.set_document(document)

document.set_parser(parser)

# 初始化文档密码

document.initialize()

if document.is_extractable:

print(True)

else:

raise PDFTextExtractionNotAllowed

# 存储文档资源

src = PDFResourceManager()

# 设备对象

device = PDFPageAggregator(src,laparams=LAParams())

# 解释器对象

inter = PDFPageInterpreter(src,device)

pages = document.get_pages()

for page in pages:

#print(page.contents)

inter.process_page(page)

layout = device.get_result()

for x in layout:

if isinstance(x, LTTextBoxHorizontal):

print(str(x.get_text()))

#t = dir(x)

#print(t)

#print(type(x))

以上代码属于搬运工 (~自带笑哭表情~~~)

上述代码中各个对象的作用:

文件解析对象(PDFParser):从文件中提取数据

文档对象(PDFDocument): 保存提取到的数据

资源对象(PDFResourceManager):保存共享内容

设备对象(PDFDevice):处理资源对象为我们所需要的格式

解释器对象(PDFPageInterpreter): 处理页面内容

eca2e88e6dac483aa58d7a40af787fd3.png

layout : 包含文档的全部内容对象主要包含:

LTPage:页面对象

LTTextBox:代表一个区域内的文字信息,包含多个LTTextLine,get_text()方法可以获得文本内容。

LTTextLine:代表一行文字信息,包含多个LTChar,get_text()方法可以获得文本内容。

LTChar:代表一个字符信息,get_text()方法可以获得文本内容。

LTAnno:代表文本中的字符的Unicode字符串。

LTFigure:代表PDF的表单对象,可以包含图形或图片。

LTImage:代表一个图片对象。

LTLine : 代表一条直线。

LTRect:代表一个矩形区域。

LTCurve:代表一条曲线。

tip:LTTextBox、LTTextLine可以分别和Horizontal、Vertical 组合表示水平方向和垂直方向

7fb3dcea6a6f46c88cf8d0215096ec70.jpg

文档对象(Document)常用属性和方法:

doc.get_outlines(): 获取文档的目录数据

doc.is_extractable(): 判断文档是否支持转文字

doc.get_pages(): 获取所有页面对象

doc.initialize():初始化文档密码

doc.set_parser():绑定文档解析对像

内容对象(LTTextBox、LTTextLine、LTImage...等等)常用属性和方法:

LTImage:

LTImage.get_rawdata(): 获取图片数据

2. fitz 包:

参考:https://blog.youkuaiyun.com/qq_15969343/article/details/81673302

3.pymupdf包:

操作pdf文件,可以实现创建pdf文件、修改pdf文件等等。。功能强大。(以后补充...)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值