#!/usr/bin/env python3 """ 诊断None路径错误的脚本 """ import os import json from pathlib import Path def check_config_files(): """检查配置文件""" print("检查配置文件:") print("=" * 40) config_files = [ "config.json", "default.json", "config.py" ] for config_file in config_files: config_path = Path(config_file) print(f"\n检查: {config_file}") if config_path.exists(): print(f"✓ 文件存在: {config_path.absolute()}") if config_file.endswith('.json'): try: with open(config_path, 'r', encoding='utf-8') as f: config_data = json.load(f) # 查找模板相关配置 template_keys = ['template', 'template_path', 'word_template', 'report_template'] for key in template_keys: if key in config_data: value = config_data[key] print(f" {key}: {value}") if value is None or value == "": print(f" ⚠️ {key} 为空值!") except Exception as e: print(f" ✗ 读取JSON失败: {e}") else: print(f"✗ 文件不存在: {config_path.absolute()}") def check_environment_variables(): """检查环境变量""" print("\n检查环境变量:") print("=" * 40) env_vars = [ 'TEMPLATE_PATH', 'WORD_TEMPLATE', 'REPORT_TEMPLATE', 'CONFIG_PATH' ] for var in env_vars: value = os.environ.get(var) print(f"{var}: {value if value else '未设置'}") if value is None: print(f" ⚠️ {var} 未设置") def check_template_files(): """检查模板文件""" print("\n检查模板文件:") print("=" * 40) # 当前目录 current_dir = Path(".") print(f"当前目录: {current_dir.absolute()}") # 查找所有docx文件 docx_files = list(current_dir.glob("*.docx")) print(f"找到 {len(docx_files)} 个docx文件:") for docx_file in docx_files: print(f" - {docx_file}") print(f" 绝对路径: {docx_file.absolute()}") print(f" 文件大小: {docx_file.stat().st_size} 字节") def simulate_path_issue(): """模拟路径问题""" print("\n模拟路径问题:") print("=" * 40) # 测试None路径会发生什么 test_paths = [None, "", "None", "template.docx"] for test_path in test_paths: print(f"\n测试路径: {repr(test_path)}") if test_path is None: print(" ⚠️ 路径为None - 这会导致COM错误!") continue if test_path == "": print(" ⚠️ 路径为空字符串 - 可能导致问题!") continue path_obj = Path(test_path) print(f" 绝对路径: {path_obj.absolute()}") print(f" 文件存在: {path_obj.exists()}") def check_report_generator_imports(): """检查report_generator的导入""" print("\n检查report_generator导入:") print("=" * 40) try: # 检查关键模块 modules_to_check = [ 'config_model', 'influx_service', 'logger' ] for module_name in modules_to_check: try: __import__(module_name) print(f"✓ {module_name} 导入成功") except ImportError as e: print(f"✗ {module_name} 导入失败: {e}") # 检查配置模型 try: from config_model import AppConfig print("✓ AppConfig 可用") except Exception as e: print(f"✗ AppConfig 导入失败: {e}") except Exception as e: print(f"✗ 检查导入时出错: {e}") def main(): print("诊断None路径错误") print("=" * 50) # 显示当前工作目录 print(f"当前工作目录: {os.getcwd()}") # 各项检查 check_config_files() check_environment_variables() check_template_files() check_report_generator_imports() simulate_path_issue() print("\n" + "=" * 50) print("诊断完成") print("\n可能的解决方案:") print("1. 检查config.json或default.json中的模板路径配置") print("2. 确保模板文件存在且路径正确") print("3. 检查代码中是否有返回None的路径获取函数") print("4. 设置正确的环境变量") if __name__ == "__main__": main()