PyPDF2.errors.DeprecationError: PdfFileReader is deprecated and was removed in PyPDF2 3.0.0. Use PdfReader instead.
时间: 2024-01-06 17:36:39
浏览: 194
感谢您指出错误!确实,最新[版本](https://geek.csdn.net/educolumn/04449e2eb7da35a9c72dce97dbf43e2a?spm=1055.2569.3001.10083)的PyPDF2库已经移除了`PdfFileReader`类,并推荐使用`Reader`类来代替。以下是使用新的类进行修改后的示例[代码](https://geek.csdn.net/educolumn/1572ef9b473b4e00f6b2bf6d428b7c27?spm=1055.2569.3001.10083):
```python
import os
from PyPDF2 import PdfReader
def rotate_pages_in_folder(folder_path):
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".pdf"):
file_path = os.path.join(root, file)
with open(file_path, "rb") as f:
pdf = PdfReader(f)
num_pages = len(pdf.pages)
if num_pages >= 2:
page1 = pdf.pages[0]
page2 = pdf.pages[1]
if page1.rotate == 90 or page2.rotate == 90:
# 页面[方向](https://geek.csdn.net/educolumn/0d81b27e827dd5e2dce9776ddcdc7f70?spm=1055.2569.3001.10083)为竖向,顺时针旋转90度
# 这里可以添加旋转操作
print(f"{file_path}: 竖向,旋转90度")
elif page1.rotate == 0 and page2.rotate == 0:
# 页面[方向](https://geek.csdn.net/educolumn/0d81b27e827dd5e2dce9776ddcdc7f70?spm=1055.2569.3001.10083)为横向
print(f"{file_path}: 横向,不变")
else:
print(f"{file_path}: 其他[方向](https://geek.csdn.net/educolumn/0d81b27e827dd5e2dce9776ddcdc7f70?spm=1055.2569.3001.10083),不做处理")
else:
print(f"{file_path}: 页面数量不足2,不做处理")
# 替换为您的文件夹路径
folder_path = "your_folder_path"
rotate_pages_in_folder(folder_path)
```