首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >pyPDF2“流意外结束”

pyPDF2“流意外结束”
EN

Stack Overflow用户
提问于 2022-03-31 17:27:02
回答 1查看 513关注 0票数 0

这是我的第一个python代码。作者传递了一个错误。这似乎是随机发生在循环过程中,通过pdf的。

try: except: pass将无法工作,因为它只会跳过该问题的文件,而不会为它生成一个输出。

strict=False似乎不适合作者。

错误:

代码语言:javascript
运行
复制
PdfReadWarning: Multiple definitions in dictionary at byte 0x6eb54 for key /PageMode [generic.py:587]
PdfReadWarning: Multiple definitions in dictionary at byte 0x75740 for key /PageMode [generic.py:587]
PdfReadWarning: Multiple definitions in dictionary at byte 0xabc13 for key /PageMode [generic.py:587]
Traceback (most recent call last):
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\kmincey.BCSBLOCAL\.vscode\extensions\ms-python.python-2022.4.0\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
    cli.main()
  File "c:\Users\kmincey.BCSBLOCAL\.vscode\extensions\ms-python.python-2022.4.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, in main
    run()
  File "c:\Users\kmincey.BCSBLOCAL\.vscode\extensions\ms-python.python-2022.4.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, in run_file
    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 268, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\kmincey.BCSBLOCAL\Desktop\Python_scripts\PDFsealer_V2.py", line 56, in <module>
    output_pdf.write(f)
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\site-packages\PyPDF2\pdf.py", line 482, in write
    self._sweepIndirectReferences(externalReferenceMap, self._root)
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\site-packages\PyPDF2\pdf.py", line 571, in _sweepIndirectReferences
    self._sweepIndirectReferences(externMap, realdata)
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, value)
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\site-packages\PyPDF2\pdf.py", line 571, in _sweepIndirectReferences
    self._sweepIndirectReferences(externMap, realdata)
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, value)
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\site-packages\PyPDF2\pdf.py", line 556, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, data[i])
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\site-packages\PyPDF2\pdf.py", line 571, in _sweepIndirectReferences
    self._sweepIndirectReferences(externMap, realdata)
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, value)
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\site-packages\PyPDF2\pdf.py", line 556, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, data[i])
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\site-packages\PyPDF2\pdf.py", line 577, in _sweepIndirectReferences
    newobj = data.pdf.getObject(data)
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\site-packages\PyPDF2\pdf.py", line 1611, in getObject
    retval = readObject(self.stream, self)
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\site-packages\PyPDF2\generic.py", line 66, in readObject
    return DictionaryObject.readFromStream(stream, pdf)
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\site-packages\PyPDF2\generic.py", line 579, in readFromStream
    value = readObject(stream, pdf)
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\site-packages\PyPDF2\generic.py", line 68, in readObject
    return readHexStringFromStream(stream)
  File "C:\Users\kmincey.BCSBLOCAL\AppData\Local\Programs\Python\Python39\lib\site-packages\PyPDF2\generic.py", line 311, in readHexStringFromStream
    raise PdfStreamError("Stream has ended unexpectedly")
PyPDF2.utils.PdfStreamError: Stream has ended unexpectedly

我读过几篇关于需要在读取器中放置strict=False以传递警告而不是错误的帖子。https://stackoverflow.com/questions/42570432/pypdf2-stream-has-ended-unexpectedlyhttps://github.com/mstamy2/PyPDF2/issues/99.然而,在大多数情况下,作者现在似乎是问题所在。

事先谢谢您的建议。

供参考的循环片段:

代码语言:javascript
运行
复制
for file in input_pdf:
    output_pdf = PdfFileWriter()
    sg.OneLineProgressMeter('My Meter', i, page_count, 'And now we Wait.....')
    PageObj = PyPDF2.PdfFileReader(open(file, "rb"), strict=False).getPage(0)
    PageObj.scaleTo(11*72, 17*72)
    PageObj.mergePage(Seal_pdf.getPage(0))
    output_pdf.addPage(PageObj)

    output_filename = f"{file}"
    f = open(output_filename, "wb+")
    output_pdf.write(f)
    i = i + 1
    f.close()
EN

回答 1

Stack Overflow用户

发布于 2022-04-06 22:29:54

由于来自@cards和@KJ的有用输入,我发现问题在于我试图覆盖一个正在使用的文件。原作仍被记在记忆中,这一事实一旦传到作者的记忆中就会败坏。只需将文件保存在不同的名称下,并编写更多的代码来清理目录,这就是我所使用的解决方案。谢谢你的协助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71696836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档