You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法用Tabula或Camelot提取PDF表格,求解决方案

问题描述

尝试用Tabula提取PDF中的表格时返回空DataFrame,且该工具对其他同类表格提取正常;后续尝试Camelot也未成功。附上使用的代码:

Tabula 代码

from tabula import read_pdf 
from tabulate import tabulate
import pandas as pd

Page_No = 1
tables = read_pdf('/content/page1.pdf', pages=Page_No, multiple_tables=True)
df1 = pd.DataFrame(tables[0])
df1

Camelot 代码

import camelot

tables2 = camelot.read_pdf('page1.pdf', flavor='lattice', pages='1')
tables2

待提取的表格截图:
待提取的PDF表格


提取建议

针对这类可能存在弱边框/无明确格子线的表格,可尝试以下方法:

1. 调整Tabula参数优化识别

Tabula默认依赖清晰格子线,可通过手动指定区域、列分割坐标或切换模式解决:

from tabula import read_pdf
import pandas as pd

# 手动指定表格区域(格式:[top, left, bottom, right],坐标需从PDF阅读器获取)
tables = read_pdf(
    '/content/page1.pdf',
    pages=1,
    area=[100, 50, 700, 800],
    columns=[150, 300, 450, 600],  # 列分隔的x坐标
    stream=True,  # 适配无格子线的流式表格
    multiple_tables=True
)
if tables:
    df1 = pd.DataFrame(tables[0])
    print(df1)
else:
    print("未检测到表格")

2. 切换Camelot的识别模式

Camelot的lattice模式依赖格子线,若表格边框模糊,改用stream模式并调整边缘检测阈值:

import camelot

tables2 = camelot.read_pdf(
    'page1.pdf',
    flavor='stream',
    pages='1',
    edge_tol=500  # 增大阈值以识别模糊边缘
)
print(tables2)
if tables2.n > 0:
    print(tables2[0].df)

3. 预处理PDF增强对比度

若PDF本身线条模糊,先通过工具增强后再提取:

  • 用Adobe Acrobat的「扫描与OCR」功能优化文档对比度
  • 用ImageMagick命令行处理:
convert -threshold 50% page1.pdf page1_enhanced.pdf

处理后再用Tabula/Camelot提取。

4. 结合OCR工具处理扫描件PDF

如果表格是图片格式的扫描件,Tabula/Camelot无法直接识别,需先做OCR:

import fitz  # PyMuPDF
import pytesseract
from PIL import Image
import pandas as pd

# 将PDF页面转为图片
doc = fitz.open('page1.pdf')
page = doc.load_page(0)
pix = page.get_pixmap()
pix.save('page1.png')

# OCR识别图片文本
text = pytesseract.image_to_string('page1.png', lang='eng')
# 解析文本为DataFrame(需根据表格格式调整分割规则)
rows = [line.split() for line in text.split('\n') if line.strip()]
df = pd.DataFrame(rows[1:], columns=rows[0])
print(df)

内容的提问来源于stack exchange,提问作者Pravin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 12:01:46