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

参考:
巧用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

参数


代码

python 复制代码
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提取表格效果会更好,但是也有少部分数据可能错行

相关推荐
kyle~3 小时前
FastAPI --- 将业务能力以 API 的形式提供给外部系统
python·fastapi·前后端
aiotto资源分享3 小时前
Python 写 JSON 文件,程序中途退出会留下什么?
java·python·json·jsondecodeerror
梯度下降者4 小时前
Python信号量,条件变量,事件学习
python·条件变量·事件·信号量·线程通信
MSTcheng.4 小时前
【AI应用实战】用 FastAPI + SSE 做一个自己的 AI 聊天网页:流式输出、多轮对话与完整源码
人工智能·python·大模型·fastapi·sse·ai聊天助手
the局外人5 小时前
Python 实战检索优化:BM25、向量与重排
数据库·后端·python
__underline__5 小时前
冷门Python知识点006篇:Python 类体里的名字,能被"凭空造出来"
后端·python
用户0332126663676 小时前
使用 Python 轻松移除 Excel 文档中的重复行
python·excel
LiaoWL1236 小时前
从零搭一个 RAG 知识库(二):换上语义 Embedding——BGE 实测与阈值重校准
python·大模型·numpy·embedding·向量检索·rag
Dmz丶6 小时前
前端转 AI 100 天 Day 48:向量数据库——100 万向量的毫秒级检索
python
青少儿编程课堂7 小时前
蓝桥杯·全国青少年信息素养大赛·电子学会Python/C++考级:中国剩余定理解析
c++·python·算法·bfs·信息学竞赛