首先借用github上的解答链接
% python3 -m venv venv
% source ./venv/bin/activate
[venv] % pip install pypdf2==1.26.0
[venv] % python test.py
1.26.0
[venv] % pip install pypdf2==1.27.3
[venv] % python test.py
1.27.3
Traceback (most recent call last):
File "test.py", line 39, in
main()
File "test.py", line 35, in main
split(pdf_data, split_info)
File "test.py", line 22, in split
pdf_writer.write(tmp_io)
File "/home/dst/Desktop/pypdf2/venv/lib/python3.8/site-packages/PyPDF2/pdf.py", line 480, in write
self._sweepIndirectReferences(externalReferenceMap, self._root)
File "/home/dst/Desktop/pypdf2/venv/lib/python3.8/site-packages/PyPDF2/pdf.py", line 572, in _sweepIndirectReferences
self._sweepIndirectReferences(externMap, realdata)
File "/home/dst/Desktop/pypdf2/venv/lib/python3.8/site-packages/PyPDF2/pdf.py", line 548, in _sweepIndirectReferences
value = self._sweepIndirectReferences(externMap, value)
File "/home/dst/Desktop/pypdf2/venv/lib/python3.8/site-packages/PyPDF2/pdf.py", line 572, in _sweepIndirectReferences
self._sweepIndirectReferences(externMap, realdata)
File "/home/dst/Desktop/pypdf2/venv/lib/python3.8/site-packages/PyPDF2/pdf.py", line 548, in _sweepIndirectReferences
value = self._sweepIndirectReferences(externMap, value)
File "/home/dst/Desktop/pypdf2/venv/lib/python3.8/site-packages/PyPDF2/pdf.py", line 557, in _sweepIndirectReferences
value = self._sweepIndirectReferences(externMap, data[i])
File "/home/dst/Desktop/pypdf2/venv/lib/python3.8/site-packages/PyPDF2/pdf.py", line 572, in _sweepIndirectReferences
self._sweepIndirectReferences(externMap, realdata)
File "/home/dst/Desktop/pypdf2/venv/lib/python3.8/site-packages/PyPDF2/pdf.py", line 548, in _sweepIndirectReferences
value = self._sweepIndirectReferences(externMap, value)
File "/home/dst/Desktop/pypdf2/venv/lib/python3.8/site-packages/PyPDF2/pdf.py", line 589, in _sweepIndirectReferences
newobj = self._sweepIndirectReferences(externMap, newobj)
File "/home/dst/Desktop/pypdf2/venv/lib/python3.8/site-packages/PyPDF2/pdf.py", line 548, in _sweepIndirectReferences
value = self._sweepIndirectReferences(externMap, value)
File "/home/dst/Desktop/pypdf2/venv/lib/python3.8/site-packages/PyPDF2/pdf.py", line 575, in _sweepIndirectReferences
if data.pdf.stream.closed:
AttributeError: 'PdfFileWriter' object has no attribute 'stream'
可以看到在版本为1.26.0时,可以正常运行,而默认安装的版本一般可能会是1.27以上,就会报错
所以要解决这个问题就只用
pip install pypdf2==1.26.0
之后就可以正常运行了
|