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

参考:
巧用Python的camelot库批量提取PDF发票信息
关于文本pdf的表格抽取

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

在这里插入图片描述
报错解决:
ModuleNotFoundError: No module named ‘Workbook’ xlwt,是版本太低,升级版本即可。 pip install --upgrade xlwt

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

下载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提取表格效果会更好,但是也有少部分数据可能错行
在这里插入图片描述