【问题标题】:python pdfminer - KeyError 'AcroForm'python pdfminer - KeyError 'AcroForm'
【发布时间】:2018-05-29 00:45:36
【问题描述】:

我有一个获取附件文件名的脚本,如果 ext 指示 pdf 文件,它会通过以下代码运行它。但是我收到下面的 KeyError。我找不到有关如何纠正或排除故障的任何信息。我之前已经通过这个脚本成功地运行了 PDF 表单,但不知道为什么它没有按预期运行。

 if ext == '.PDF' or ext == '.pdf':
      item_field_list = []
      item_number = str(random.randint(1000000, 9999999))
      #try:
      with tempfile.NamedTemporaryFile() as tmp:
           verify_item = 0
           tmp.write(part.get_payload(decode=True))
           parser = PDFParser(tmp)
           doc = PDFDocument(parser)
           fields = resolve1(doc.catalog['AcroForm'])['Fields']

这是回溯:

Traceback (most recent call last):
  File "distributionitemimport.py", line 87, in <module>
    fields = resolve1(doc.catalog['AcroForm'])['Fields']
KeyError: 'AcroForm'

当我执行print(doc.catalog) 时,我得到以下信息:

{'MarkInfo': {'Marked': True}, 'Lang': b'en-US', 'Type': /'Catalog', 'StructTree
Root': <PDFObjRef:162>, 'Pages': <PDFObjRef:2>}

【问题讨论】:

  • 如果您在有问题的行之前执行print(doc.catalog),您会看到什么打印?
  • @alecxe 我已经更新为包含。谢谢!
  • 当您调用resolve1(doc.catalog) 时,resolve1 会返回什么?
  • @Ajax1234 看起来像:&lt;class 'dict'&gt;
  • 我的猜测是它要么用密码保护,要么加密,或两者兼而有之。不幸的是,我目前不知道该怎么做,并且正在努力解决同样的问题。

标签: python keyerror pdfminer


【解决方案1】:

问题是在您的 pdf 集合中,有些 pdf 没有 AcroForm。首先,删除所有没有AcroForm的pdf

我建议创建一个新文件夹并放入一个包含 AcroForm 的 pdf 并检查您的代码。它会工作

【讨论】:

    猜你喜欢
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 2017-11-22
    • 2022-01-21
    • 2020-12-27
    • 1970-01-01
    相关资源
    最近更新 更多