【问题标题】:Tabula-py skips first page from PDF and misses some tabular dataTabula-py 跳过 PDF 的第一页并遗漏了一些表格数据
【发布时间】:2020-03-27 09:33:51
【问题描述】:

我正在使用 Python (3.8.1) 和 tabula-py (2.1.0) (https://tabula-py.readthedocs.io/en/latest/tabula.html#tabula.io.build_options) 从基于文本的 PDF 文件(AWS 月度账单报告)中提取表格。

下面是显示了 PDF 文件的示例(第一页底部和第二页顶部)。


Python 脚本如下所示:

from tabula import read_pdf
from tabulate import tabulate

df = read_pdf(
   "my_report.pdf",
   output_format="dataframe",
   multiple_tables=True,
   pages="all",
   silent=True,
   # TODO: area = (x_left, x_right, y_left, y_right) # ?
)

print(tabulate(df))


生成以下输出:

---  ---------------------------------------------------------------------------  ---------------------  ---------
  0  region                                                                       nan                    nan
  1  AWS CloudTrail APS2-PaidEventsRecorded                                       nan                    $3.70
  2  0.00002 per paid event recorded in Asia Pacific (Sydney)                     184,961.000 Events     $3.70
  3  region                                                                       nan                    nan
  4  Asia Pacific (Tokyo)                                                         nan                    $3.20

我的想法是必须正确设置 area 选项,因为有时会省略顶部和最左侧的数据。是这样吗?如果是,如何在 PDF 文件中找到所有表格数据的正确区域?

提前致谢。

【问题讨论】:

  • PostScript 是一种页面布局语言。它几乎不保留源文档的结构(分部、章节等)。因此,识别 PDF 中的表格更像是一门艺术,而不是一门科学。 PostScript 中没有table 标签。 tabula-py 需要简单地从布局中推断出一个表的存在。并且没有简单的方法可以从 PDF 中发现第 2 页上的表格是第 1 页上的表格的延续,除非它有重复的标题。也许您可以在 github.com/chezou/tabula-py/issues 上将此问题报告为问题。
  • @BoarGules 好点。我将报告这个问题。你有什么更好的解决方案来从这个 PDF 中提取表格数据到一些数据框、CSV 格式或类似的吗?

标签: python python-3.x tabula tabula-py


【解决方案1】:

尝试使用参数“guess=False”。

【讨论】:

    【解决方案2】:

    我设法通过扩展正在搜索的数据的位置来解决这个问题:

    # get locations from page 2 data:
    tables = read_pdf("my_report.pdf", output_format="json", pages=2, silent=True)
    top = tables[0]["top"]
    left = tables[0]["left"]
    bottom = tables[0]["height"] + top
    right = tables[0]["width"] + left
    # Expand location borders slightly:
    test_area = [top - 20, left - 20, bottom + 10, right + 10]
    
    # Now read_pdf gives all data with the following call:
    
    df = read_pdf(
       "my_report.pdf",
       multiple_tables=True,
       pages="all",
       silent=True,
       area = test_area
    )
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多