python pyqt5 读取文件夹的pdf进行分割,旋转等操作,操作完后创建一个与源文件夹相同的目录输出到此目录。
时间: 2025-06-21 14:26:48 浏览: 13
### 使用 Python 和 PyQt5 实现 PDF 文件批量处理
为了实现这一目标,可以采用 PyPDF2 库来执行具体的 PDF 处理任务,并利用 PyQt5 构建图形用户界面 (GUI),以便于交互式地选择文件夹、指定操作以及查看进度。
#### 导入库
首先安装必要的库:
```bash
pip install pypdf2 pyqt5
```
接着导入所需的模块:
```python
import os
from PyPDF2 import PdfReader, PdfWriter
from PyQt5.QtWidgets import QApplication, QFileDialog, QVBoxLayout, QWidget, QPushButton, QLabel
```
#### 创建 GUI 组件
构建一个简易的窗口应用程序,允许用户选取源文件夹和目的文件夹:
```python
class MainWindow(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
self.label_source = QLabel('未选择源文件夹')
self.button_select_source = QPushButton('选择源文件夹')
self.button_process_pdfs = QPushButton('开始处理')
layout.addWidget(self.label_source)
layout.addWidget(self.button_select_source)
layout.addWidget(self.button_process_pdfs)
self.setLayout(layout)
self.source_folder_path = ''
self.output_folder_path = ''
self.init_ui()
def init_ui(self):
self.setWindowTitle('PDF 批量处理器')
self.setGeometry(100, 100, 300, 150)
self.button_select_source.clicked.connect(self.select_source_folder)
self.button_process_pdfs.clicked.connect(self.process_pdfs)
def select_directory():
dialog = QFileDialog()
folder_path = str(QFileDialog.getExistingDirectory(dialog, "Select Directory"))
return folder_path
```
#### 定义核心逻辑函数
编写用于遍历文件夹树形结构并应用所需转换的方法:
```python
def process_pdf(file_path, output_dir):
reader = PdfReader(open(file_path,"rb"))
# 对每一页进行特定的操作比如旋转90度
writer = PdfWriter()
for page_num in range(len(reader.pages)):
page = reader.pages[page_num]
rotated_page = page.rotate(90) # 这里假设需要顺时针旋转90度
writer.add_page(rotated_page)
base_name = os.path.basename(file_path)
new_file_path = os.path.join(output_dir, base_name)
with open(new_file_path,'wb') as out_f:
writer.write(out_f)
def traverse_and_process(source_root, target_root):
for root, dirs, files in os.walk(source_root):
relative_path = os.path.relpath(root, source_root)
current_target = os.path.join(target_root, relative_path)
if not os.path.exists(current_target):
os.makedirs(current_target)
pdf_files = [f for f in files if f.lower().endswith('.pdf')]
for file in pdf_files:
full_path = os.path.join(root, file)
try:
process_pdf(full_path, current_target)
except Exception as e:
print(f'Error processing {full_path}:', str(e))
```
#### 集成所有部分
将上述组件组合起来形成完整的解决方案:
```python
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
@window.button_select_source.clicked.connect
def on_click_select_source():
selected_folder = select_directory()
if selected_folder != '':
window.source_folder_path = selected_folder
window.label_source.setText(selected_folder)
@window.button_process_pdfs.clicked.connect
def on_click_start_processing():
if window.source_folder_path and window.output_folder_path:
traverse_and_process(window.source_folder_path, window.output_folder_path)
print("Processing completed.")
else:
print("Please choose both input and output directories.")
window.show()
app.exec_()
```
此代码片段展示了如何通过PyQt5创建一个简单易用的应用程序接口,让用户能够轻松完成对多个PDF文档的一系列预设变换操作[^1]。 同时也体现了使用PyPDF2来进行实际文件修改的能力[^2]。
阅读全文
相关推荐







