camelot pdf提取表格实践(记录)

慈云数据 2024-03-19 技术支持 66 0

参考:

巧用Python的camelot库批量提取PDF发票信息

关于文本pdf的表格抽取

AttributeError: module ‘camelot‘ has no attribute ‘read_pdf‘及类似问题解决办法

camelot 参数

https://blog.csdn.net/INTSIG/article/details/123000010

在这里插入图片描述

报错解决:

ModuleNotFoundError: No module named ‘Workbook’ xlwt,是版本太低,升级版本即可。 pip install --upgrade xlwt

介绍:

camelot方法有两种解析模式:流解析(stream)、格子解析(lattice),其中格子解析能够保留表格完整的样式,对于复杂表格来说要优于流解析模式。同时,camelot方法默认格子解析(lattice),而采用这种解析方式,需要安装ghostscript。

camelot是一个可以从可编辑的pdf文档中抽取表格的开源框架,与pdfplumber相比,其功能完备性差了点,除了表格抽取之外,并不能用它从pdf文档中解析出字符、单词、文本、线等较为低层次的对象

下载ghostscript https://www.ghostscript.com/releases/gsdnld.html

参数

在这里插入图片描述

在这里插入图片描述

代码

import camelot
import pandas as pd
# 使用Camelot读取PDF文件中的表格
tables = camelot.read_pdf('pdf.pdf', pages='all', flavor='lattice')
# 将所有表格转换为 DataFrame 并合并
all_data = pd.concat([table.df for table in tables], ignore_index=True)
all_data.to_excel('all_data.xlsx',index=False)

识别效果:不太理想,文本排序有问题。pdfplumber提取表格效果会更好,但是也有少部分数据可能错行

在这里插入图片描述

微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon