C:\Users\Administrator\AppData\Local\Programs\Python\Python311\python.exe "C:/Users/Administrator/Documents/WPSDrive/1206454978/WPS云盘/Python/python Xiang Mu/测试读取.py" Traceback (most recent call last): File "C:\Users\Administrator\Documents\WPSDrive\1206454978\WPS云盘\Python\python Xiang Mu\测试读取.py", line 9, in <module> pdf_reader = PyPDF2.PdfFileReader(pdf_file) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyPDF2\_reader.py", line 1974, in __init__ deprecation_with_replacement("PdfFileReader", "PdfReader", "3.0.0") File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyPDF2\_utils.py", line 369, in deprecation_with_replacement deprecation(DEPR_MSG_HAPPENED.format(old_name, removed_in, new_name)) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyPDF2\_utils.py", line 351, in deprecation raise DeprecationError(msg) PyPDF2.errors.DeprecationError: PdfFileReader is deprecated and was removed in PyPDF2 3.0.0. Use PdfReader instead.
时间: 2023-07-08 12:50:08 浏览: 346
解决git:fatal:Unable to create”…/.git/index.lock” 的错误
这个报错是因为 PyPDF2 库的版本更新了,`PdfFileReader` 已经被废弃,需要使用 `PdfReader` 替换。你可以将代码中的 `PdfFileReader` 替换为 `PdfReader`,如下所示:
```python
import PyPDF2
import chardet
# 打开 PDF 文件
with open('filename.pdf', 'rb') as pdf_file:
pdf_reader = PyPDF2.PdfReader(pdf_file)
# 获取 PDF 文件中的所有页面
pages = len(pdf_reader.pages)
# 遍历每一页,获取页面的文本内容
for i in range(pages):
page = pdf_reader.pages[i]
text = page.extract_text()
# 检测编码格式并将文本内容转换成正确的编码格式
detected_encoding = chardet.detect(text)
text = text.decode(detected_encoding['encoding']).encode('utf-8')
# 将文本内容保存到文件中
with open(f'output_{i}.txt', 'w', encoding='utf-8') as output_file:
output_file.write(text)
```
希望这次能够运行成功!
阅读全文