#!/usr/bin/env python3 """ 修复模板路径问题的测试脚本 """ import os from pathlib import Path def find_template_files(): """查找所有可能的模板文件""" print("查找模板文件:") print("=" * 40) # 可能的搜索路径 search_paths = [ Path("E:/docx_creatorV2"), Path("."), Path("f:/PyPro/docx_creator"), Path(os.getcwd()) ] template_files = [] for search_path in search_paths: print(f"搜索路径: {search_path}") if search_path.exists(): # 查找.docx文件 docx_files = list(search_path.glob("*.docx")) for docx_file in docx_files: if docx_file.is_file(): template_files.append(docx_file) print(f" 找到: {docx_file}") else: print(f" 路径不存在") return template_files def test_template_access(): """测试模板文件访问""" print("\n测试模板文件访问:") print("=" * 40) template_files = find_template_files() if not template_files: print("未找到任何模板文件") return False for template_file in template_files[:3]: # 只测试前3个 print(f"\n测试文件: {template_file}") print(f"绝对路径: {template_file.resolve()}") print(f"文件存在: {template_file.exists()}") print(f"文件大小: {template_file.stat().st_size if template_file.exists() else 'N/A'} 字节") # 测试Word打开 try: import win32com.client word_app = win32com.client.Dispatch("Word.Application") word_app.Visible = False abs_path = str(template_file.resolve()) print(f"尝试打开: {abs_path}") doc = word_app.Documents.Open(abs_path) print("✓ 成功打开") # 检查表格 tables_count = doc.Tables.Count print(f"表格数量: {tables_count}") doc.Close(SaveChanges=False) word_app.Quit() return True except Exception as e: print(f"✗ 打开失败: {e}") try: word_app.Quit() except: pass return False def main(): print("模板路径修复测试") print("=" * 50) # 显示当前工作目录 print(f"当前工作目录: {os.getcwd()}") # 查找模板文件 template_files = find_template_files() # 测试访问 if template_files: success = test_template_access() if success: print("\n✓ 找到可用的模板文件") else: print("\n✗ 模板文件无法正常打开") else: print("\n✗ 未找到任何模板文件") print("建议:") print("1. 确认模板文件位置") print("2. 检查文件权限") print("3. 确认文件名正确") if __name__ == "__main__": main()