python PyPDF2.utils.PdfReadError: Illegal character in Name Object
环境说明
PyCharm 2020.2.3 x64
Python 3.7
PyPDF2模块
报错场景
在使用PyPDF2模块将一个PDF文件分割成多个PDF文件时,出现报错
python PyPDF2.utils.PdfReadError: Illegal character in Name Object
报错原因
该错误是由于读取的PDF文件中包含多种编码导致
报错信息如下
Traceback (most recent call last):
File "E:\Program_Practice\Python\pachong\venv\lib\site-packages\PyPDF2\generic.py", line 484, in readFromStream
return NameObject(name.decode('utf-8'))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcb in position 8: invalid continuation byte
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:/Program_Practice/Python/pachong/test/Day_12/pdf_segmentation.py", line 19, in <module>
pdf_writer.write(out)
File "E:\Program_Practice\Python\pachong\venv\lib\site-packages\PyPDF2\pdf.py", line 482, in write
self._sweepIndirectReferences(externalReferenceMap, self._root)
File "E:\Program_Practice\Python\pachong\venv\lib\site-packages\PyPDF2\pdf.py", line 571, in _sweepIndirectReferences
self._sweepIndirectReferences(externMap, realdata)
File "E:\Program_Practice\Python\pachong\venv\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
value

在Python 3.7环境中使用PyPDF2模块处理PDF文件时遇到PdfReadError,原因是PDF文件包含多种编码。通过修改PyPDF2源码,包括在generic.py文件中添加处理多编码的代码,以及在utils.py文件中注释和替换特定代码段,可以解决这个问题。
最低0.47元/天 解锁文章
31万+





