【问题标题】:Extract table with invisible lines from PDF从 PDF 中提取带有不可见线条的表格
【发布时间】:2018-09-28 12:16:48
【问题描述】:

问题陈述:

我有一个 PDF,其结构类似于表格,但线条不可见。请在下面找到示例:

上图是我的表格在其中一个 PDF 页面中的外观。

我的研究

  1. How to extract table as text from the PDF using Python? -- 看完了这个问题,看到了所有的答案。没有帮助

  2. Tabula:尝试了 tabula API,但它只提取标题而不是文本,可能是因为没有行。

  3. 我可以将整个 pdf 转换为文本,然后尝试以某种方式使用正则表达式或数据操作来提取它。但这可能非常乏味且耗时。此外,随着 PDF 的更改,整个编码必须再次完成。

问

他们是否有任何 API 或 Python 包可以帮助我做到这一点(Windows 和 Python 3.x)?

【问题讨论】:

  • 对不起,我的错。应该读; how-to-extract-table-as-text-from-the-pdf-using-python
  • 已经这样做了...你有没有完整阅读我的问题...这个链接是我研究的一部分?
  • 这个链接回答你的问题!同样没有minimal reproducible example,这个问题将被hold。
  • 它不是因为我的表格有不可见的边界,并且提供的链接中的所有答案都认为表格有边界。
  • @El_1988:可能没有通用的解决方案!我尝试做的是通过 PyMuPdf 或任何其他软件包阅读 PDF……看看他们是如何破坏表格的,然后编写代码/逻辑来提取相关数据!!

标签: python pdf extract


【解决方案1】:

您需要使用一个包来为您提供 PDF 中文本的 x 和 y 坐标。 PyMuPDF 或 pdfminer 将是我的建议。然后,您需要以编程方式确定您遇到的每个文本块所在的行和列。

【讨论】:

    【解决方案2】:

    我通过tabula-py解决了这个问题

    conda install tabula-py
    

    和

    >>> import tabula
    >>> area = [70, 30, 750, 570] # Seems to have to be done manually
    >>> page2 = tabula.read_pdf("nar_2021_editorial-2.pdf", guess=False, lattice=False, 
                     stream=True, multiple_tables=False, area=area, pages="all",
                       ) # `tabula` doc explains params very well
    >>> page2
    

    我得到了这个结果

    > 'pages' argument isn't specified.Will extract only from page 1 by default. [      
    > ShortTitle                                              Text  \  0    
    > Arena3Dweb         3D visualisation of multilayered networks     1    
    > Aviator       Monitoring the availability of web services     2       
    > b2bTools  Predictions for protein biophysical features and     3      
    > NaN                                their conservation     4         
    > BENZ WS          Four-level Enzyme Commission (EC) number     ..      
    > ...                                               ...     68 
    > miRTargetLink2              miRNA target gene and target pathway    
    > 69             NaN                                          networks  
    > 70       mmCSM-PPI            Effects of multiple point mutations on  
    > 71             NaN                      protein-protein interactions  
    > 72        ModFOLD8           Quality estimates for 3D protein models  
    > 
    >  
    >                                                 URL    0                    http://bib.fleming.gr/Arena3D    1         
    > https://www.ccb.uni-saarland.de/aviator    2                   
    > https://bio2byte.be/b2btools/    3                                    
    > NaN    4                 https://benzdb.biocomp.unibo.it/    ..       
    > ...    68  https://www.ccb.uni-saarland.de/mirtargetlink2    69       
    > NaN    70          http://biosig.unimelb.edu.au/mmcsm ppi    71       
    > NaN    72       https://www.reading.ac.uk/bioinf/ModFOLD/      [73
    > rows x 3 columns]]
    

    这是一个可迭代的obj,所以你可以通过for row in page2:来操作它

    希望对你有帮助

    【讨论】:

    • 这个回复对我帮助很大。我希望我能请你吃一顿丰盛的晚餐。
    • @DanceParty 很高兴听到这个消息。所以请投票给我,因为我是这个社区的新手,需要一些声誉。谢谢~
    【解决方案3】:

    尝试使用 Camelot 并指定您的表格没有这样的行:

    tables = camelot.read_pdf('file.pdf', flavor = 'stream')
    

    更多信息请参考文档https://camelot-py.readthedocs.io/en/master/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 2020-11-12
      • 1970-01-01
      相关资源
      最近更新 更多