【问题标题】:How do I get which page is the table extracted from using tabula-py?如何获取使用 tabula-py 提取的表格是哪个页面?
【发布时间】:2020-05-14 19:29:44
【问题描述】:

我目前正在使用tabula.read_pdf() 从 pdf 中提取表格。但是,没有关于该表来自哪个页面的信息。一种方法是通过传入tabula.read_pdf()pages 参数来获取页面总数并迭代每个页面。然而,这是极其低效的。以下是一些解释,我在这里使用一个示例pdf http://www.annualreports.com/HostedData/AnnualReports/PDF/NASDAQ_AMZN_2019.pdf

%%time
for i in range(1,88):
    tables = read_pdf(pdf_path, pages=i, stream=True)
# CPU times: user 803 ms, sys: 686 ms, total: 1.49 s
# Wall time: 3min 4s

%%time
tables = read_pdf(pdf_path, pages='all', stream=True)
# CPU times: user 402 ms, sys: 171 ms, total: 573 ms
# Wall time: 21.2 s

【问题讨论】:

  • 您能否解释一下为什么它效率低下?
  • 嗨 lammuratc,我刚刚编辑了我的问题并添加了有关迭代每个页面与使用 pages='all' 所用时间的更多详细信息。
  • 我明白你的意思。我对pdf文件的库不太了解。你不能只迭代一次pdf并保存所有表格吗?或者也许你可以使用另一种语言,你知道 Python 不是最快的 :)

标签: python tabula tabula-py


【解决方案1】:

你可以用 camelot 代替 tabula。

Camelot 的一个很酷的功能是,您还可以获得每个表格的“解析报告”,提供准确度指标、找到表格的页面以及表格中存在的空白百分比.

file = "your_file_path"
tables = camelot.read_pdf(file, pages = "1-end")
# get the 3rd-indexed-table
tables[3].df
# get the information of the third table, you will find the page
tables[3].parsing_report

参考:http://theautomatic.net/2019/05/24/3-ways-to-scrape-tables-from-pdfs-with-python/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多