python編程:tabula、pdfplumber、camelot進行表格數據識別


python編程:tabula、pdfplumber、camelot進行表格數據識別

版權聲明:本文為博主原創文章,歡迎轉載,請注明出處 https://blog.csdn.net/mouday/article/details/85057226

本文就目前python圖表識別的庫進行測試
1、tabula
2、pdfplumber
3、camelot

准備數據

excel:names.xlsx,兩個表格
表格1:所有字段都被線條包圍
表格2:最外層沒有線條包圍
在這里插入圖片描述

將excel另存為pdf:names.pdf
在這里插入圖片描述

1、tabula

java項目:https://github.com/tabulapdf

自帶可視化界面的pdf提取表格數據工具:
https://tabula.technology/

python接口:https://github.com/chezou/tabula-py

安裝:

pip install tabula-py
  • 1

依賴:
Java 7, 8

代碼示例:

import tabula tabula.convert_into( input_path="source/names.pdf", output_path="source/names.csv", output_format='csv' ) 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

轉換出來的names.csv,發現只有表格1被提取出來了,而且不規范,中間多了逗號

"姓名",年齡,性別
"李雷",,20 男
"韓梅梅",,23 女
"趙小三",,25 女

  • 1
  • 2
  • 3
  • 4
  • 5

2、pdfplumber

github: https://github.com/jsvine/pdfplumber

安裝

pip install pdfplumber
  • 1

代碼示例:

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
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

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

3、camelot

github: https://github.com/socialcopsdev/camelot

安裝:

pip install camelot-py[cv]
  • 1

示例

import camelot tables = camelot.read_pdf("source/names.pdf") tables.export("source/names.csv") 
  • 1
  • 2
  • 3
  • 4

生成2個文件:
source/names-page-1-table-1.csv

"姓名","年齡","性別"
"李雷","20 男",""
"韓梅梅","23 女",""
"趙小三","25 女",""

  • 1
  • 2
  • 3
  • 4
  • 5

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

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

  • 1
  • 2
  • 3
  • 4
  • 5

發現表格2的內容被解析出來了,不過兩個表格的內容都錯位了

經過測試后,發現這3個庫對表格識別都不是太好

總結

庫名 說明
tabula 能提取完整表格,提取結果不規范
pdfplumber 能提取完整表格,提取結果較為規范
camelot 能提取完整表格和不完整表格,提取結果不規范


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM