【问题标题】:Tabula-py read_pdf_with_template() methodTabula-py read_pdf_with_template() 方法
【发布时间】:2025-12-08 22:00:02
【问题描述】:

我正在尝试将文档的特定部分作为表格读取。它采用表格结构,但在单元格、行或列之间没有分隔线。

我成功地使用了带有areacolumn 参数的read_pdf() 方法。我可以准确地指定表格的开始和结束位置以及列的划分位置。

但是我的 pdf 在每个页面上都有多个不同大小的表格,没有明确的标记来识别它们,我必须使用这些参数。

我在 Github 存储库问题 here 中发现了 read_pdf_with_template() 方法,并在 documentationpull requestexample notebook 中了解了更多信息。

但没有提到如何构建teamplate.json 以及我可以使用哪些参数或它们的含义。

我尝试将area 坐标插入x1, y1, x2, y2,在方法参数中传递列列表,并将heightwidth 参数与表的大小一起传递。

但它正在拾取 pdf 的顶部中心部分,这不等于我在反向计算所有内容时插入的任何坐标。

这是我要阅读的页面(我删除了一些敏感数据)

这里是代码sn-ps

import tabula

tables = tabula.read_pdf_with_template(input_path = "test.pdf", template_path = "template.json", columns=[195, 310, 380])

print(tables[0])
[
    {
        "page": 1,
        "extraction_method": "stream",
        "x1": 225,
        "x2": 35,
        "y1": 375,
        "y2": 565,
        "width": 525,
        "height": 400
    }
]

【问题讨论】:

    标签: python tabula tabula-py


    【解决方案1】:

    我只是个笨蛋。

    模板不是您手动生成的。它们应该由here 提到的tabula 应用程序生成。

    只需从official website 下载表格。启动应用程序后,它非常简单。手动点击并拖动每个页面上的每个表格,然后点击顶部的下载模板按钮。

    【讨论】: