SKILL.md
readonly只读
name
batch-processor
description
批量处理多个文档,支持并行执行
version
1.0
批量处理器技能
概述
本技能支持高效的文档批量处理——可并行转换、转换、提取或分析数百个文件,并实时跟踪进度。
使用方法
- 描述您想要完成的任务
- 提供所需的输入数据或文件
- 我将执行相应的操作
示例提示:
- "将100个PDF转换为Word文档"
- "提取文件夹中所有图片的文本"
- "批量重命名并整理文件"
- "批量更新文档页眉/页脚"
领域知识
批量处理模式
输入: [文件1, 文件2, ..., 文件N]
│
▼
┌─────────────┐
│ 并行工作器 │ ← 同时处理多个文件
└─────────────┘
│
▼
输出: [结果1, 结果2, ..., 结果N]
Python实现
from concurrent.futures import ProcessPoolExecutor, as_completed
from pathlib import Path
from tqdm import tqdm
def process_file(file_path: Path) -> dict:
"""处理单个文件。"""
# 在此处编写处理逻辑
return {"path": str(file_path), "status": "success"}
def batch_process(input_dir: str, pattern: str = "*.*", max_workers: int = 4):
"""处理目录中所有匹配的文件。"""
files = list(Path(input_dir).glob(pattern))
results = []
with ProcessPoolExecutor(max_workers=max_workers) as executor:
futures = {executor.submit(process_file, f): f for f in files}
for future in tqdm(as_completed(futures), total=len(files)):
file = futures[future]
try:
result = future.result()
results.append(result)
except Exception as e:
results.append({"path": str(file), "error": str(e)})
return results
# 使用示例
results = batch_process("/documents/invoices", "*.pdf", max_workers=8)
print(f"已处理 {len(results)} 个文件")
错误处理与断点续传
import json
from pathlib import Path
class BatchProcessor:
def __init__(self, checkpoint_file: str = "checkpoint.json"):
self.checkpoint_file = checkpoint_file
self.processed = self._load_checkpoint()
def _load_checkpoint(self):
if Path(self.checkpoint_file).exists():
return json.load(open(self.checkpoint_file))
return {}
def _save_checkpoint(self):
json.dump(self.processed, open(self.checkpoint_file, "w"))
def process(self, files: list, processor_func):
for file in files:
if str(file) in self.processed:
continue # 跳过已处理的文件
try:
result = processor_func(file)
self.processed[str(file)] = {"status": "success", **result}
except Exception as e:
self.processed[str(file)] = {"status": "error", "error": str(e)}
self._save_checkpoint() # 支持断点续传
最佳实践
- 使用进度条(tqdm)提供用户反馈
- 为长时间任务实现检查点机制
- 设置合理的工作器数量(CPU核心数)
- 记录失败信息以便后续审查
安装
# 安装所需依赖
pip install python-docx openpyxl python-pptx reportlab jinja2






