#!/usr/bin/env python3 """ 快速修复模板路径问题 """ import json from pathlib import Path def create_last_state_file(): """创建包含模板路径的状态文件""" print("创建.last_state.json文件") print("=" * 40) # 查找可用的模板文件 template_candidates = [ "template-001.docx", "template-20250926.docx", "1000泵跑合.docx" ] template_path = None for candidate in template_candidates: if Path(candidate).exists(): template_path = str(Path(candidate).absolute()) print(f"找到模板文件: {template_path}") break if not template_path: print("未找到模板文件,创建默认配置") template_path = str(Path("template-001.docx").absolute()) # 创建状态文件 last_state = { "template": template_path, "config": "config.json" } state_file = Path(".last_state.json") with open(state_file, 'w', encoding='utf-8') as f: json.dump(last_state, f, ensure_ascii=False, indent=2) print(f"✓ 状态文件已创建: {state_file.absolute()}") print(f"模板路径: {template_path}") def check_config_json(): """检查并修复config.json""" print("\n检查config.json") print("=" * 40) config_file = Path("config.json") if config_file.exists(): try: with open(config_file, 'r', encoding='utf-8') as f: config = json.load(f) print("✓ config.json 存在且可读取") except Exception as e: print(f"✗ config.json 读取失败: {e}") config = {} else: print("config.json 不存在,创建默认配置") config = {} # 确保基本配置存在 if "influx" not in config: config["influx"] = { "url": "", "org": "", "token": "", "bucket": "", "measurement": "" } if "placeholders" not in config: config["placeholders"] = {} if "experimentProcess" not in config: config["experimentProcess"] = { "headers": [], "rows": [], "scriptFile": "", "scriptName": "", "remark": "" } # 保存配置 with open(config_file, 'w', encoding='utf-8') as f: json.dump(config, f, ensure_ascii=False, indent=2) print(f"✓ config.json 已更新: {config_file.absolute()}") def create_simple_template(): """创建一个简单的测试模板""" print("\n创建简单测试模板") print("=" * 40) template_file = Path("template-001.docx") if template_file.exists(): print(f"✓ 模板文件已存在: {template_file}") return try: from docx import Document # 创建新文档 doc = Document() # 添加标题 doc.add_heading('测试报告模板', 0) # 添加表格 table = doc.add_table(rows=25, cols=10) table.style = 'Table Grid' # 在第一个单元格添加scriptTable1标记 table.cell(0, 0).text = 'scriptTable1' # 保存文档 doc.save(str(template_file)) print(f"✓ 简单模板已创建: {template_file.absolute()}") except ImportError: print("✗ python-docx 未安装,无法创建模板") print("请手动创建模板文件或安装: pip install python-docx") except Exception as e: print(f"✗ 创建模板失败: {e}") def main(): print("快速修复模板路径问题") print("=" * 50) # 显示当前目录 print(f"当前目录: {Path.cwd()}") # 1. 创建状态文件 create_last_state_file() # 2. 检查配置文件 check_config_json() # 3. 创建简单模板(如果需要) create_simple_template() print("\n" + "=" * 50) print("修复完成!") print("\n建议:") print("1. 重新启动应用程序") print("2. 在应用中点击'浏览'选择正确的模板文件") print("3. 如果仍有问题,请运行诊断脚本") if __name__ == "__main__": main()