【问题标题】:Using PDFminer as a library: "AttributeError: 'NoneType' object has no attribute 'getobj'"使用 PDFminer 作为库:“AttributeError: 'NoneType' 对象没有属性 'getobj'”
【发布时间】:2013-02-17 08:43:08
【问题描述】:

我正在编写一个用于上传 PDF 文件并在此过程中解析它们的脚本。对于解析,我使用PDFminer

为了将文件转换为 PDFMiner 文档,我使用以下功能,完全按照上面链接中的说明进行操作:

def load_document(self, _file = None):
    """turn the file into a PDFMiner document"""
    if _file == None:
        _file = self.options['file']

    parser = PDFParser(_file)
    doc = PDFDocument()
    doc.set_parser(parser)
    if self.options['password']:
        password = self.options['password']
    else:
        password = ""
    doc.initialize(password)
    if not doc.is_extractable:
        raise ValueError("PDF text extraction not allowed")

    return doc

预期的结果当然是一个不错的PDFDocument 实例,但我得到了一个错误:

Traceback (most recent call last):
  File "bzk_pdf.py", line 45, in <module>
    cli.run_cli(BZKPDFScraper)
  File "/home/toon/Projects/amcat/amcat/scripts/tools/cli.py", line 61, in run_cli
    instance = cls(options)
  File "/home/toon/Projects/amcat/amcat/scraping/pdf.py", line 44, in __init__
    self.doc = self.load_document()
  File "/home/toon/Projects/amcat/amcat/scraping/pdf.py", line 56, in load_document
    doc.set_parser(parser)
  File "/usr/local/lib/python2.7/dist-packages/pdfminer/pdfparser.py", line 327, in set_parser
    self.info.append(dict_value(trailer['Info']))
  File "/usr/local/lib/python2.7/dist-packages/pdfminer/pdftypes.py", line 132, in dict_value
    x = resolve1(x)
  File "/usr/local/lib/python2.7/dist-packages/pdfminer/pdftypes.py", line 60, in resolve1
    x = x.resolve()
  File "/usr/local/lib/python2.7/dist-packages/pdfminer/pdftypes.py", line 49, in resolve
    return self.doc.getobj(self.objid)
AttributeError: 'NoneType' object has no attribute 'getobj'

我不知道去哪里找,也没有发现其他人有同样的问题。

一些可能有帮助的额外信息:

【问题讨论】:

  • 小点,但我认为你的意思是 django 版本的 1.4.3。
  • 有人回答了吗?或者试图重现问题?我真的需要一个答案...

标签: python pdf


【解决方案1】:

经过一些实验,我发现我漏掉了一行:

parser.set_document(doc)

添加该行后,该功能现在可以工作了。

在我看来库设计很糟糕,但可能是我遗漏了一些东西,这只是修补了错误。

不管怎样,我现在有一个 PDF 文档,里面有我需要的数据。

这是最终结果:

def load_document(self, _file = None):
    """turn the file into a PDFMiner document"""
    if _file == None:
        _file = self.options['file']

    parser = PDFParser(_file)
    doc = PDFDocument()
    parser.set_document(doc)
    doc.set_parser(parser)

    if 'password' in self.options.keys():
        password = self.options['password']
    else:
        password = ""

    doc.initialize(password)

    if not doc.is_extractable:
        raise ValueError("PDF text extraction not allowed")

    return doc

【讨论】:

    【解决方案2】:

    尝试打开文件并将其发送到解析器,如下所示:

    with open(_file,'rb') as f:
        parser = PDFParser(f)
        # your normal code here
    

    您现在的操作方式,我怀疑您将文件名作为字符串发送。

    【讨论】:

    • 很抱歉,事实并非如此。正如我最后所说,_file 是一个 Django File 对象,使用普通文件具有相同的效果。
    猜你喜欢
    • 2019-01-01
    • 2021-12-26
    • 2019-07-23
    • 2018-05-13
    • 2020-09-07
    • 2017-05-03
    • 2023-03-16
    • 2018-07-14
    • 2013-06-16
    相关资源
    最近更新 更多