![](/img/trans.png)
[英]Getting Assertion error while reading the PDF file python - pypdf2
[英]AssertionError in python while saving a pdf file generated using PyPDF2
我想將給定的 PDF 頁面拆分為單獨的 PDF。
這是我寫的代碼,但在此,使用open()
和.write()
函數保存文件時出現錯誤:AssertionError
from PyPDF2 import PdfFileReader, PdfFileWriter
pdf = PdfFileReader("input.pdf") # this is the source pdf
for page in range(pdf.getNumPages()):
pdf_writer = PdfFileWriter()
pdf_writer.addPage(pdf.getPage(page))
output = f'output{page}.pdf' # getting pdf name to be saved as
with open(output, 'wb') as output_pdf:
pdf_writer.write(output_pdf)
這是完整的錯誤:
Traceback (most recent call last):
File "test.py", line 11, in <module>
pdf_writer.write(output_pdf)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 482, in write
self._sweepIndirectReferences(externalReferenceMap, self._root)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 571, in _sweepIndirectReferences
self._sweepIndirectReferences(externMap, realdata)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
value = self._sweepIndirectReferences(externMap, value)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 571, in _sweepIndirectReferences
self._sweepIndirectReferences(externMap, realdata)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
value = self._sweepIndirectReferences(externMap, value)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 556, in _sweepIndirectReferences
value = self._sweepIndirectReferences(externMap, data[i])
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 571, in _sweepIndirectReferences
self._sweepIndirectReferences(externMap, realdata)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
value = self._sweepIndirectReferences(externMap, value)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
value = self._sweepIndirectReferences(externMap, value)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
value = self._sweepIndirectReferences(externMap, value)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 577, in _sweepIndirectReferences
newobj = data.pdf.getObject(data)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\pdf.py", line 1611, in getObject
retval = readObject(self.stream, self)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\generic.py", line 66, in readObject
return DictionaryObject.readFromStream(stream, pdf)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\PyPDF2\generic.py", line 604, in readFromStream
assert "/Length" in data
AssertionError
在運行本教程中的每個示例時,我都參考了這篇博客文章教程 pypdf2我遇到了同樣的問題。
使用:Windows 10,Python 3.7.3,PyPDF2是最新版本
我不知道原因的細節(我對 PyPDF2 不再那么流利了),但我相信您必須為每個新頁面創建一個新的 PyPDF2 PdfFileReader 和 PdfFileWriter 。 您可以嘗試以下方法嗎?
from PyPDF2 import PdfFileReader, PdfFileWriter
input_pdf = "input.pdf"
# Get number of pages before looping
pages = PdfFileReader(input_pdf).getNumPages()
for page in range(pages):
pdf_reader = PdfFileReader(input_pdf)
pdf_writer = PdfFileWriter()
pdf_writer.addPage(pdf.getPage(page))
output = f'output{page}.pdf' # getting pdf name to be saved as
with open(output, 'wb') as output_pdf:
pdf_writer.write(output_pdf)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.