import os import re import shutil # ====== 配置 ====== input_dir = r"E:\Train\EM 18 REJ" # 原始 XML 文件夹路径 output_dir = r"E:\Train\EM18Output" # 修改后 XML 文件输出路径 offset = 1450 # 要裁掉的像素值 # 创建输出文件夹(如果不存在) os.makedirs(output_dir, exist_ok=True) def process_xml(file_path, output_path, offset): with open(file_path, 'r', encoding='utf-8') as f: content = f.read() if "2800" in content: new_width = 2800 - offset new_content = content.replace("2800", f"{new_width}") with open(output_path, 'w', encoding='utf-8') as f: f.write(new_content) print(f"✅ 修改并保存: {os.path.basename(file_path)} → 宽度 {new_width}") else: # 直接复制未修改的文件 shutil.copy(file_path, output_path) print(f"↪ 保持原样: {os.path.basename(file_path)}") for filename in os.listdir(input_dir): if filename.lower().endswith(".xml"): input_path = os.path.join(input_dir, filename) output_path = os.path.join(output_dir, filename) process_xml(input_path, output_path, offset)