您当前的位置: 首页 >  Python

顺其自然~

暂无认证

  • 1浏览

    0关注

    1317博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

顺其自然~ 发布时间:2021-12-30 11:34:47 ,浏览量:1

本文就目前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        能提取完整表格和不完整表格,提取结果不规范

关注
打赏
1662339380
查看更多评论
立即登录/注册

微信扫码登录

0.0400s