簡體   English   中英

pyPDF2“流意外結束”

[英]pyPDF2 "Stream has ended unexpectedly"

這是我的第一個 python 代碼。 編寫器傳遞錯誤。 這似乎是在循環瀏覽 pdf 的過程中隨機發生的。

try: except: pass將不起作用,因為它只會跳過有問題的文件,而不會為其生成 output。

strict=False似乎對作者不起作用。

錯誤:

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 這在大多數情況下都有效,但是,作者現在似乎是問題所在。

在此先感謝您的任何建議。

For循環片段供參考:

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()

由於來自@cards 和@KJ 的有用輸入,我能夠發現問題是我試圖覆蓋正在使用的文件。 事實上,原件仍被捆綁在 memory 中,一旦到達作者手中,它就會損壞。 我采用的解決方案是簡單地以不同的名稱保存文件並編寫更多代碼來清理目錄。 感謝您的協助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM