Tabula-py 跳过 PDF 的第一页并遗漏一些表格数据

Tabula-py skips first page from PDF and misses some tabular data

我正在使用 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                    .70
  2  0.00002 per paid event recorded in Asia Pacific (Sydney)                     184,961.000 Events     .70
  3  region                                                                       nan                    nan
  4  Asia Pacific (Tokyo)                                                         nan                    .20

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

提前致谢。

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

# 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
)

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