【发布时间】: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 看起来像:
<class 'dict'> -
我的猜测是它要么用密码保护,要么加密,或两者兼而有之。不幸的是,我目前不知道该怎么做,并且正在努力解决同样的问题。