From d156fdb2d855bcd7ca29e8f717e2aec16e948284 Mon Sep 17 00:00:00 2001 From: Misaka_Company Date: Mon, 11 May 2026 09:44:55 +0800 Subject: [PATCH] fix: address code review issues for Access support - Fix import_vba_access() to actually save database via DoCmd.Save() - Update stale module docstring in extract_vba.py - Validate file extensions in get_file_type() instead of silently defaulting to excel - Scan both Excel/ and Access/ directories in interactive mode Co-Authored-By: Claude Opus 4.6 --- extract_vba.py | 24 ++++++++++++++---------- import_vba.py | 10 +++++++--- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/extract_vba.py b/extract_vba.py index 1287902..3a94058 100644 --- a/extract_vba.py +++ b/extract_vba.py @@ -1,7 +1,7 @@ """ VBA代码提取工具 -从xlsm文件中提取模块和类模块代码,分类保存到VBA文件夹 -自动清理Attribute信息并生成元数据JSON文件 +从Excel(.xlsm)或Access(.accdb)文件中提取VBA代码,分类保存到VBA文件夹 +自动清理Attribute信息 """ import os @@ -42,11 +42,16 @@ def get_file_type(file_path: Path) -> str: Returns: 'access' 或 'excel' + + Raises: + ValueError: 不支持的文件扩展名 """ ext = file_path.suffix.lower() if ext in ACCESS_EXTENSIONS: return 'access' - return 'excel' + if ext in EXCEL_EXTENSIONS: + return 'excel' + raise ValueError(f"不支持的文件类型: {ext}(支持: .xlsm, .xls, .xlsb, .accdb, .mdb)") class VBAExtractor: @@ -461,15 +466,14 @@ def main(): print() else: # 交互模式:查找支持的文件 - excel_dir = Path("Excel") - if not excel_dir.exists(): - print("错误: 未找到Excel文件夹") - return + all_files = [] + for scan_dir in [Path("Excel"), Path("Access")]: + if scan_dir.exists(): + for ext in ["*.xlsm", "*.accdb", "*.mdb"]: + all_files.extend(scan_dir.glob(ext)) - # 同时扫描 Excel 和 Access 文件 - all_files = list(excel_dir.glob("*.xlsm")) + list(excel_dir.glob("*.accdb")) + list(excel_dir.glob("*.mdb")) if not all_files: - print("错误: Excel文件夹中没有.xlsm或.accdb文件") + print("错误: 未找到.xlsm或.accdb文件(请检查Excel/或Access/文件夹)") return # 如果有多个文件,让用户选择 diff --git a/import_vba.py b/import_vba.py index 8c1d2cc..028a1e7 100644 --- a/import_vba.py +++ b/import_vba.py @@ -50,11 +50,16 @@ def get_file_type(file_path: Path) -> str: Returns: 'access' 或 'excel' + + Raises: + ValueError: 不支持的文件扩展名 """ ext = file_path.suffix.lower() if ext in ACCESS_EXTENSIONS: return 'access' - return 'excel' + if ext in EXCEL_EXTENSIONS: + return 'excel' + raise ValueError(f"不支持的文件类型: {ext}(支持: .xlsm, .xls, .xlsb, .accdb, .mdb)") class VBAImporter: @@ -411,8 +416,7 @@ class VBAImporter: print("\n正在保存...") try: - import time - time.sleep(1) # 等待 VBE 完成 + access.DoCmd.Save() print("已保存更改。") except Exception as e: print(f"保存时出错: {e}")