Camelot: PDF表格提取利器
camelot Camelot: PDF Table Extraction for Humans 项目地址: https://gitcode.com/gh_mirrors/ca/camelot
1. 项目介绍
Camelot 是一个用 Python 编写的库,它让任何人都能轻松地从 PDF 文件中提取表格。Camelot 不仅提供了强大的表格识别功能,还允许用户基于准确度等指标调整和优化提取结果。每个提取的表格都可以转化为 pandas DataFrame,方便集成到 ETL 和数据分析工作流中。
2. 项目快速启动
安装
使用 conda:
conda install -c conda-forge camelot-py
使用 pip(需要先安装 tk 和 ghostscript):
pip install camelot-py[cv]
从源代码安装(需要先安装依赖):
git clone https://github.com/camelot-dev/camelot
cd camelot
pip install ".[cv]"
示例代码
import camelot
# 读取 PDF 文件
tables = camelot.read_pdf('example.pdf')
# 导出 CSV 文件
tables.export('tables.csv', f='csv')
# 获取第一个表格的数据框架
df = tables[0].df
3. 应用案例和最佳实践
提取表格
使用 Camelot 提取 PDF 中的表格非常简单。首先,你需要确定 PDF 文件是文本格式的(即你可以用鼠标在 PDF 阅读器中选择文本)。以下是提取表格的基本步骤:
import camelot
# 读取 PDF 文件
tables = camelot.read_pdf('example.pdf')
# 检查表格
for table in tables:
print(table)
# 导出表格到 CSV
tables.export('extracted_tables.csv', f='csv')
处理提取结果
Camelot 允许你基于各种指标,如准确度和空白度,来评估和筛选提取的表格。
# 获取准确度大于 90% 的表格
high_accuracy_tables = [table for table in tables if table.parsing_report['accuracy'] > 90]
# 导出高准确度的表格
high_accuracy_tables.export('high_accuracy_tables.csv', f='csv')
4. 典型生态项目
Camelot 的设计中考虑了与其他工具和库的集成。例如,你可以将提取的数据直接转化为 pandas DataFrame,从而方便地与 Jupyter Notebook、数据分析和数据库操作等工具链进行集成。此外,Camelot 还可以与 Excalibur 这样的 web 界面工具配合使用,为用户提供更丰富的交互体验。
以上就是 Camelot 的简单介绍和快速入门。通过 Camelot,开发者可以大大简化从 PDF 文件提取表格数据的流程,提高工作效率。
camelot Camelot: PDF Table Extraction for Humans 项目地址: https://gitcode.com/gh_mirrors/ca/camelot
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考