""" 检查 .doc 文件转换环境 检查系统是否支持自动转换,并提供转换指导 """ import os import sys from pathlib import Path def check_pywin32(): """检查是否安装了 pywin32""" try: import win32com.client return True, "已安装" except ImportError: return False, "未安装" def check_word(): """检查是否安装了 Microsoft Word""" try: import win32com.client word = win32com.client.Dispatch("Word.Application") word.Quit() return True, "已安装且可用" except Exception as e: return False, f"未安装或不可用: {str(e)}" def find_doc_files(): """查找所有 .doc 文件""" project_root = Path(__file__).parent templates_dir = project_root / "模板" / "原始模板" doc_files = [] if templates_dir.exists(): for root, dirs, files in os.walk(templates_dir): for file in files: if file.lower().endswith('.doc'): doc_files.append(Path(root) / file) return doc_files def main(): print("="*80) print("检查 .doc 文件转换环境") print("="*80) print() # 检查 pywin32 has_pywin32, pywin32_status = check_pywin32() print(f"1. pywin32 状态: {pywin32_status}") if not has_pywin32: print(" → 安装命令: pip install pywin32") print() # 检查 Word has_word, word_status = check_word() print(f"2. Microsoft Word 状态: {word_status}") if not has_word: print(" → 需要安装 Microsoft Word(不是 WPS)") print() # 查找 .doc 文件 doc_files = find_doc_files() print(f"3. 找到 {len(doc_files)} 个 .doc 文件:") if doc_files: for i, doc_file in enumerate(doc_files[:10], 1): # 只显示前10个 rel_path = doc_file.relative_to(Path(__file__).parent) print(f" {i}. {rel_path}") if len(doc_files) > 10: print(f" ... 还有 {len(doc_files) - 10} 个文件") else: print(" 未找到 .doc 文件") print() # 总结和建议 print("="*80) print("转换建议") print("="*80) if has_pywin32 and has_word: print("✓ 环境就绪,可以自动转换") print(" 运行: python process_templates.py") elif has_word and not has_pywin32: print("⚠ 已安装 Word,但未安装 pywin32") print(" 解决方案:") print(" 1. 安装 pywin32: pip install pywin32") print(" 2. 或使用批处理脚本: 批量转换doc到docx.bat") print(" 3. 或手动转换文件") elif not has_word: print("✗ 未安装 Microsoft Word") print(" 解决方案:") print(" 1. 安装 Microsoft Word(不是 WPS)") print(" 2. 或使用批处理脚本: 批量转换doc到docx.bat(需要 Word)") print(" 3. 或手动转换文件(推荐)") print(" 4. 或使用在线转换工具") else: print("✗ 环境不支持自动转换") print(" 解决方案:") print(" 1. 手动转换文件(推荐)") print(" 2. 使用在线转换工具") print() print("详细说明请查看: doc转换说明.md") print("="*80) if __name__ == '__main__': try: main() except KeyboardInterrupt: print("\n\n用户中断") sys.exit(1) except Exception as e: print(f"\n错误: {e}") import traceback traceback.print_exc() sys.exit(1)