关于 table-extractor
table-extractor 是一款 Python 工具,可从 PDF 和图像中提取表格并转为结构化数据。它支持扫描内容的 OCR、多页处理及多种导出格式,适用于文档与数据整理。
主要功能
- 01PDF 和图像表格提取
- 02扫描图像 OCR 提取
- 03多页文档处理
- 04CSV、Excel 和 JSON 导出
- 05检测表格边界与列
适用场景
- 01从 PDF 报告提取表格
- 02将扫描表单转换为结构化数据
- 03按指定页面范围处理 PDF
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ dkyazzentwatwa/ chatgpt-skills --skill table-extractor安装后在 Claude Code 中输入/table-extractor确认已加载。
附带文件scripts/requirements.txtscripts/table_extractor.py
详细介绍
表格提取器
将 PDF 和图像中的表格提取为结构化数据格式。
功能
- PDF 表格:从数字版 PDF 中提取表格
- 图像表格:基于 OCR 从图像中提取表格
- 多个表格:提取文档中的所有表格
- 格式导出:输出 CSV、Excel、JSON
- 表格检测:自动检测表格边界
- 列对齐:智能检测列
- 多页处理:处理整个 PDF 文档
快速开始
from table_extractor import TableExtractor
extractor = TableExtractor()
# Extract from PDF
extractor.load_pdf("document.pdf")
tables = extractor.extract_all()
# Save first table to CSV
tables[0].to_csv("table.csv")
# Extract from image
extractor.load_image("scanned_table.png")
table = extractor.extract_table()
print(table)
CLI 用法
# Extract from PDF
python table_extractor.py --input document.pdf --output tables/
# Extract specific pages
python table_extractor.py --input document.pdf --pages 1-3 --output tables/
# Extract from image
python table_extractor.py --input scan.png --output table.csv
# Export to Excel
python table_extractor.py --input document.pdf --format xlsx --output tables.xlsx
# With OCR for scanned PDFs
python table_extractor.py --input scanned.pdf --ocr --output tables/
API 参考
TableExtractor 类
class TableExtractor:
def __init__(self)
# Loading
def load_pdf(self, filepath: str, pages: List[int] = None) -> 'TableExtractor'
def load_image(self, filepath: str) -> 'TableExtractor'
# Extraction
def extract_table(self, page: int = 0) -> pd.DataFrame
def extract_all(self) -> List[pd.DataFrame]
def extract_page(self, page: int) -> List[pd.DataFrame]
# Detection
def detect_tables(self, page: int = 0) -> List[Dict]
def get_table_count(self) -> int
# Configuration
def set_ocr(self, enabled: bool = True, lang: str = "eng") -> 'TableExtractor'
def set_column_detection(self, mode: str = "auto") -> 'TableExtractor'
# Export
def to_csv(self, tables: List, output_dir: str) -> List[str]
def to_excel(self, tables: List, output: str) -> str
def to_json(self, tables: List, output: str) -> str
支持的格式
输入
- PDF 文档(基于文本和扫描版)
- 图像:PNG、JPEG、TIFF、BMP
- 包含表格的屏幕截图
输出
- CSV(每个表格对应一个文件)
- Excel(多个工作表)
- JSON(表格数组)
- Pandas DataFrame
表格检测
# Detect tables without extracting
tables_info = extractor.detect_tables(page=0)
# Returns:
# [
# {"index": 0, "rows": 10, "cols": 5, "bbox": (x1, y1, x2, y2)},
# {"index": 1, "rows": 8, "cols": 3, "bbox": (x1, y1, x2, y2)}
# ]
示例工作流
PDF 报告中的表格
extractor = TableExtractor()
extractor.load_pdf("quarterly_report.pdf")
# Extract all tables
tables = extractor.extract_all()
# Export each to CSV
for i, table in enumerate(tables):
table.to_csv(f"table_{i}.csv", index=False)
扫描文档
extractor = TableExtractor()
extractor.set_ocr(enabled=True, lang="eng")
extractor.load_image("scanned_form.png")
table = extractor.extract_table()
print(table)
依赖项
- pdfplumber>=0.10.0
- pillow>=10.0.0
- pandas>=2.0.0
- pytesseract>=0.3.10(用于 OCR)
- opencv-python>=4.8.0
评论
支持 Markdown