从PDF文档中提取表格非常困难,因为PDF不包含语义层。
Camelot
您可以尝试使用camelot
,甚至可以与其Web界面excalibur
结合使用:
>>> import camelot
>>> tables = camelot.read_pdf('foo.pdf')
>>> tables
<TableList n=1>
>>> tables.export('foo.csv', f='csv', compress=True) # json, excel, html, markdown, sqlite
>>> tables[0]
<Table shape=(7, 7)>
>>> tables[0].parsing_report
{
'accuracy': 99.02,
'whitespace': 12.24,
'order': 1,
'page': 1
}
>>> tables[0].to_csv('foo.csv') # to_json, to_excel, to_html, to_markdown, to_sqlite
>>> tables[0].df # get a pandas DataFrame!
另请参阅python-camelot
Tabula
tabula
可以通过以下方式安装
pip install tabula-py
但是它需要Java,因为tabula-py
只是Java项目的包装器。
使用方法如下:
import tabula
# Read pdf into list of DataFrame
dfs = tabula.read_pdf("test.pdf", pages='all')
另请参阅:
AWS Textract
我最近没有尝试过,但AWS Textract声称:
Amazon Textract可以从文档中提取表格,并提取表格内的单元格、合并单元格和列标题。
PdfPlumber
pdfplubmer表格提取方法:
import pdfplumber
pdf = pdfplumber.open("example.pdf")
page = pdf.pages[0]
page.extract_table()
另请参阅