Pdfplumber 无法识别表 python [重复]

2024-03-03

我使用 Pdfplumber 提取第 2 页第 3 部分的表格(通常)。但它只适用于某些 pdf,其他则不起作用。对于失败的 pdf 文件,似乎 Pdfplumber 读取的是按钮表而不是我想要的表。

我怎样才能拿到桌子? pdf 的链接不起作用:pdfA http://www.epc.shell.com/docs/GSAP_msds_01259319.PDF

有效的pdf链接:pdfB http://www.msds.exxonmobil.com/IntApps/psims/Download.aspx?ID=743681

这是我的代码:

import pdfplumber
pdf = pdfplumber.open("/Users/chueckingmok/Desktop/selenium/Shell Omala 68.pdf")
page = pdf.pages[1]
table=page.extract_table()

import pandas as pd
df = pd.DataFrame(table[1:], columns=table[0])
df

and the result is enter image description here

But the table I want in page 2 is enter image description here

但是,此代码适用于 pdfB(我上面提到过)。

顺便说一句,我想要每个 pdf 中的表格位于第 3 节中。

有人可以帮忙吗?

非常感谢 琼

Updated: 我刚刚找到了一个很好的包来提取 pdf 文件,没有任何问题。 该软件包是 fitz,也称为 PyMuPDF。


嘿,这是该问题的正确解决方案,但首先请阅读我下面的一些观点

  • 好吧,您使用 pdfplumber 进行表格提取,但我认为您应该阅读有关表格设置的内容,表格的设置有很多,当您根据需要阅读它们时,您肯定会从那里找到答案。PdfPlumber API - 用于表提取的就在这里 https://github.com/jsvine/pdfplumber#extracting-tables
  • 截至目前,我在下面为您的问题提供了完美的解决方案,但首先正确检查 pdfplumber API 的文档,您肯定可以从那里找到所有答案,并且我确信将来您不需要询问有关使用表提取的问题pdfplumber 因为您肯定会从那里找到有关表格提取以及其他内容(例如文本提取、单词提取等)的所有解决方案。
  • 为了更好地理解表设置,您还可以使用可视化调试,这是 pdfplumber 的最佳功能,用于了解表设置对表的确切作用以及如何使用表设置提取表。表的可视化调试 https://github.com/jsvine/pdfplumber/blob/stable/examples/notebooks/extract-table-nics.ipynb

以下是您问题的解决方案,

import pandas as pd
import pdfplumber 
pdf = pdfplumber.open("GSAP_msds_01259319.pdf")
p1 = pdf.pages[1]
table = p1.extract_table(table_settings={"vertical_strategy": "lines", 
                                         "horizontal_strategy": "text", 
                                         "snap_tolerance": 4,})
df = pd.DataFrame(table[1:], columns=table[0])
df

查看上述代码的输出 https://i.stack.imgur.com/YFv32.jpg

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Pdfplumber 无法识别表 python [重复] 的相关文章

随机推荐