项目进度: 0%
自动化工具

文件批处理工具

项目概述

本项目将开发一个带有图形界面的文件批处理工具,支持文件重命名、格式转换、压缩等功能。

项目特点:

  • 图形化操作界面
  • 批量文件处理
  • 多种处理功能
  • 实时进度显示
  • 可扩展的插件系统

技术栈说明

核心技术

  • Python - 编程语言
  • PyQt6 - GUI框架
  • pathlib - 路径处理
  • pillow - 图像处理

其他工具

  • pyinstaller - 打包工具
  • python-magic - 文件类型检测
  • chardet - 编码检测

项目结构

file_processor/
├── main.py
├── ui/
│   ├── main_window.py
│   └── widgets/
├── core/
│   ├── processor.py
│   └── utils.py
├── plugins/
│   ├── rename.py
│   └── convert.py
└── resources/
    └── icons/

界面设计

使用PyQt6设计用户界面:

# ui/main_window.py
from PyQt6.QtWidgets import *
from PyQt6.QtCore import *

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('文件批处理工具')
        self.resize(800, 600)
        
        # 创建主布局
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        layout = QVBoxLayout(central_widget)
        
        # 添加文件列表
        self.file_list = QListWidget()
        layout.addWidget(self.file_list)
        
        # 添加操作按钮
        button_layout = QHBoxLayout()
        self.add_btn = QPushButton('添加文件')
        self.process_btn = QPushButton('开始处理')
        button_layout.addWidget(self.add_btn)
        button_layout.addWidget(self.process_btn)
        layout.addLayout(button_layout)
        
        # 添加进度条
        self.progress = QProgressBar()
        layout.addWidget(self.progress)
        
        # 绑定事件
        self.add_btn.clicked.connect(self.add_files)
        self.process_btn.clicked.connect(self.start_process)

文件操作

实现基本的文件操作功能:

# core/processor.py
from pathlib import Path
from PIL import Image

class FileProcessor:
    def __init__(self):
        self.files = []
    
    def add_file(self, path):
        self.files.append(Path(path))
    
    def rename_file(self, file_path, new_name):
        path = Path(file_path)
        new_path = path.parent / new_name
        path.rename(new_path)
        return new_path
    
    def convert_image(self, image_path, format):
        with Image.open(image_path) as img:
            new_path = image_path.with_suffix(f'.{format}')
            img.save(new_path, format=format)
            return new_path

批处理功能

实现批量处理逻辑:

# core/processor.py
from concurrent.futures import ThreadPoolExecutor

class BatchProcessor:
    def __init__(self, processor, max_workers=4):
        self.processor = processor
        self.executor = ThreadPoolExecutor(max_workers)
    
    def process_files(self, operation, **kwargs):
        futures = []
        for file in self.processor.files:
            future = self.executor.submit(
                operation, file, **kwargs
            )
            futures.append(future)
        return futures
    
    def rename_batch(self, pattern):
        def rename_op(file, idx):
            new_name = pattern.format(
                name=file.stem,
                ext=file.suffix,
                index=idx
            )
            return self.processor.rename_file(
                file, new_name
            )
        
        return self.process_files(
            rename_op,
            enumerate(self.processor.files)
        )

进度监控

实现进度跟踪和状态更新:

# ui/main_window.py
from PyQt6.QtCore import QThread, pyqtSignal

class ProcessThread(QThread):
    progress = pyqtSignal(int)
    finished = pyqtSignal()
    error = pyqtSignal(str)

    def __init__(self, processor, operation):
        super().__init__()
        self.processor = processor
        self.operation = operation

    def run(self):
        try:
            futures = self.operation()
            total = len(futures)
            completed = 0
            
            for future in futures:
                future.result()  # 等待完成
                completed += 1
                progress = (completed / total) * 100
                self.progress.emit(progress)
            
            self.finished.emit()
        except Exception as e:
            self.error.emit(str(e))

打包发布

使用PyInstaller打包应用:

# 打包命令
pyinstaller --name="文件批处理工具" \
            --windowed \
            --icon=resources/icons/app.ico \
            --add-data "resources:resources" \
            main.py

# requirements.txt
PyQt6==6.5.2
Pillow==10.0.0
python-magic==0.4.27
chardet==5.2.0
pyinstaller==5.13.2