0. unstructured 支持的文件类型
"csv", "doc", "docx", "epub", "image", "md", "msg", "odt", "org", "pdf", "ppt", "pptx", "rtf", "rst", "tsv", "xlsx"
1. 安装系统依赖
sudo apt install libmagic-dev poppler-utils tesseract-ocr libreoffice pandoc
2. 安装 python 模块
pip install "unstructured[all-docs]"
reference: https://docs.unstructured.io/open-source/installation/full-installation
3. 使用 unstructured 从PDF文件中提取表格的两种方法
方法1:使用partition_pdf
使用partition_pdf从PDF文件中提取表格,将infer_table_structure参数设置为True,将strategy参数设置为hi_res。
用法:
from unstructured.partition.pdf import partition_pdf
fname = "example-docs/layout-parser-paper.pdf"
elements = partition_pdf(filename=fname,
infer_table_structure=True,
strategy='hi_res',
)
tables = [el for el in elements if el.category == "Table"]
print(tables[0].text)
print(tables[0].metadata.text_as_html)
方法2:使用Auto Partition
默认情况下,从所有文件类型中提取表格。使用Auto Partition或Unstructured API参数从PDF和图像中提取表格,只需将strategy参数设置为hi_res。
用法:
from unstructured.partition.auto import partition
filename = "example-docs/layout-parser-paper.pdf"
elements = partition(filename=filename,
strategy='hi_res',
)
tables = [el for el in elements if el.category == "Table"]
print(tables[0].text)
print(tables[0].metadata.text_as_html)