python识别pdf表格_python编程:tabula、pdfplumber、camelot进行表格数据识别

本文对比了Python中用于表格识别的三个库:tabula、pdfplumber和camelot,通过实际案例展示了它们在处理不同格式表格时的表现,发现各有优缺点,尤其是对表格边框敏感。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文就目前python图表识别的库进行测试

1、tabula

2、pdfplumber

3、camelot

准备数据

excel:names.xlsx,两个表格

表格1:所有字段都被线条包围

表格2:最外层没有线条包围

将excel另存为pdf:names.pdf

1、tabula

安装:

pip install tabula-py

依赖:

Java 7, 8

代码示例:

import tabula

tabula.convert_into(input_path="source/names.pdf", output_path="source/names.csv", output_format='csv')

转换出来的names.csv,发现只有表格1被提取出来了,而且不规范,中间多了逗号

"姓名",年龄,性别
"李雷",,20 男
"韩梅梅",,23 女
"赵小三",,25 女

2、pdfplumber

安装

pip install pdfplumber

代码示例:

import pdfplumber

import pandas as pd

with pdfplumber.open("source/names.pdf") as pdf: 
# 获取第一页 
first_page = pdf.pages[0] 
# 解析文本 
text = first_page.extract_text() 
print(text) 
# 解析表格 
tables = first_page.extract_tables() 
for table in tables: 
    print(table) 
    # df = pd.DataFrame(table[1:], columns=table[0]) 
    for row in table: 
        for cell in row: 
            print(cell, end="\t|") 
    print() 
""" 表格1: 姓名 年龄 性别 李雷 20 男 韩梅梅 23 女 赵小三 25 女 Table2: Name Age Gender Tom 30 Male Jack 33 Male Jone 31 Female [['姓名', '年龄', '性别'], ['李雷', '20', '男'], ['韩梅梅', '23', '女'], ['赵小三', '25', '女']] 姓名 |年龄 |性别 | 李雷 |20 |男 | 韩梅梅 |23 |女 | 赵小三 |25 |女 | [['30'], ['33']] 30 | 33 | """

文本解析的很全,只有表格1解析完全了,表格2只是解析了有框的部分

3、camelot

安装:

pip install camelot-py[cv]

示例

import camelot

tables = camelot.read_pdf("source/names.pdf") tables.export("source/names.csv")

生成2个文件:

source/names-page-1-table-1.csv

"姓名","年龄","性别"
"李雷","20 男",""
"韩梅梅","23 女",""
"赵小三","25 女",""

source/names-page-1-table-2.csv

"Name","Age","Gender"
"Tom","","30 Male"
"Jack","","33 Male"
"Jone","","31 Female"

发现表格2的内容被解析出来了,不过两个表格的内容都错位了

经过测试后,发现这3个库对表格识别都不是太好

总结

库名             说明

tabula           能提取完整表格,提取结果不规范

pdfplumber  能提取完整表格,提取结果较为规范

camelot        能提取完整表格和不完整表格,提取结果不规范

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值