【问题标题】:Trouble using pdfminer.six while parsing pdf files解析 pdf 文件时使用 pdfminer.six 时出现问题
【发布时间】:2018-07-19 12:59:55
【问题描述】:

我正在尝试使用 pdfminer.six 从 pdf 中提取文本,我按照here 中提到的以下代码进行操作

import pdfminer
import io

def extract_raw_text(pdf_filename):
    output = io.StringIO()
    laparams = pdfminer.layout.LAParams()

    with open(pdf_filename, "rb") as pdffile:
        pdfminer.high_level.extract_text_to_fp(pdffile, output, laparams=laparams)

    return output.getvalue()

print(extract_raw_text('simple1.pdf'))

但它会产生错误

Traceback (most recent call last):
  File "extract.py", line 13, in <module>
    print(extract_raw_text('simple1.pdf'))
  File "extract.py", line 6, in extract_raw_text
    laparams = pdfminer.layout.LAParams()
AttributeError: module 'pdfminer' has no attribute 'layout'

我只是想从 pdf 中提取整个文本,我们将不胜感激。

【问题讨论】:

    标签: python-3.x pdf text-extraction


    【解决方案1】:

    这解决了我的问题。

    pip install --upgrade camelot-py
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题! 可能这是新更新的问题,因为 python 没有将额外的文件识别为模块,因为它们没有很好地分类。

      所以你只需要直接导入特定的文件,你可以通过三种方式来做:

      在您的代码中(整个模块)

      不要使用import pdfminer,而是导入您想要使用的特定模块

      import pdfminer.layout
      import pdfminer.high_level
      

      这样您就可以像在
      laparams = pdfminer.layout.LAParams() 中那样直接访问所有模块的类


      在您的代码中(特定类/函数)

      同样的逻辑适用,但在这里,我们将只选择我们想要在每个模块内部使用的特定类(在您的情况下,您使用了类/函数 LAParams()extract_text_to_fp

      所以你会这样做:

      from pdfminer.layout import LAParams
      from pdfminer.high_level import extract_text_to_fp
      


      在模块本身上(为每次使用而修复)

      这是一个杀手级解决方案,但不是最佳解决方案,因为每次更新模块时您可能会丢失这些更改。如果你经常使用这个模块,那么它会很有用。

      1. 找到您的站点包位置 在你的终端上写python -m site,你就会找到所有的路径。寻找这样完成的人...lib/python3.6/site-packages

      2. 找到你的pdfminer模块,打开文件夹并打开__init__.py文件

      3. 为您希望预加载的所有模块编写代码,例如:

        导入 pdfminer.layout 导入pdfminer.high_level

      现在,每次您使用 import pdfminer 时,这些模块也会被预先加载,因此您可以按照上面编写的方式运行您的代码,它会运行。

      【讨论】:

      • 太好了,@Filtered!
      猜你喜欢
      • 2023-04-09
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-19
      • 2011-06-13
      相关资源
      最近更新 更多