add: enhance metadata file handling in import_vba.py for improved path configuration and error messages
This commit is contained in:
@@ -20,6 +20,14 @@ except ImportError:
|
||||
print("请运行: pip install pywin32")
|
||||
sys.exit(1)
|
||||
|
||||
# ==================== 配置区域 ====================
|
||||
# 在此配置元数据文件路径(vba_metadata.json)
|
||||
# 支持相对路径(相对于脚本所在目录)或绝对路径
|
||||
# 元数据文件包含 VBA 代码的映射信息,导入器会根据此文件将代码导入到对应的 xlsm 文件中
|
||||
# 留空或设为None则使用默认模式(自动查找脚本所在目录下的VBA文件夹)
|
||||
TARGET_METADATA_FILE = r"C:\Users\Administrator\Desktop\新BOM\AutoBOM\VBA\vba_metadata.json" # 例如: r"VBA\vba_metadata.json" 或 r"D:\path\to\vba_metadata.json"
|
||||
# =================================================
|
||||
|
||||
# 常量定义
|
||||
METADATA_FILE = "vba_metadata.json"
|
||||
VBA_DIR_NAME = "VBA"
|
||||
@@ -270,22 +278,70 @@ def main():
|
||||
print()
|
||||
|
||||
script_dir = Path(__file__).parent
|
||||
vba_dir = script_dir / VBA_DIR_NAME
|
||||
metadata_path = vba_dir / METADATA_FILE
|
||||
|
||||
if not metadata_path.exists():
|
||||
print(f"错误: 找不到元数据文件: {metadata_path}")
|
||||
return
|
||||
# 检查是否配置了目标元数据文件
|
||||
if TARGET_METADATA_FILE and TARGET_METADATA_FILE.strip():
|
||||
# 使用配置的元数据文件路径
|
||||
target_path = Path(TARGET_METADATA_FILE)
|
||||
|
||||
print(f"读取元数据: {metadata_path}")
|
||||
# 如果是相对路径,则相对于脚本所在目录
|
||||
if not target_path.is_absolute():
|
||||
target_path = script_dir / target_path
|
||||
|
||||
if not target_path.exists():
|
||||
print(f"错误: 配置的元数据文件不存在: {target_path}")
|
||||
return
|
||||
|
||||
if not target_path.name == METADATA_FILE:
|
||||
print(f"警告: 文件名不是 {METADATA_FILE}: {target_path.name}")
|
||||
|
||||
metadata_path = target_path
|
||||
print(f"使用配置的元数据文件: {metadata_path.name}")
|
||||
print()
|
||||
else:
|
||||
# 默认模式:查找脚本所在目录下的 VBA 文件夹
|
||||
vba_dir = script_dir / VBA_DIR_NAME
|
||||
metadata_path = vba_dir / METADATA_FILE
|
||||
|
||||
if not metadata_path.exists():
|
||||
print(f"错误: 找不到元数据文件: {metadata_path}")
|
||||
print()
|
||||
print("提示:")
|
||||
print(" 1. 确保已运行 extract_vba.py 提取 VBA 代码")
|
||||
print(" 2. 或在脚本顶部配置 TARGET_METADATA_FILE 指定元数据文件路径")
|
||||
return
|
||||
|
||||
print(f"读取元数据: {metadata_path}")
|
||||
|
||||
# 显示目标文件信息
|
||||
print("=" * 60)
|
||||
print("警告: 此操作将覆盖目标 Excel 文件中的 VBA 代码。")
|
||||
choice = input("\n确认继续? (y/n): ").lower().strip()
|
||||
|
||||
if choice != 'y':
|
||||
print("操作已取消")
|
||||
return
|
||||
|
||||
importer = VBAImporter(str(metadata_path))
|
||||
importer.import_vba()
|
||||
|
||||
# 显示导入目标信息
|
||||
print()
|
||||
print(f"目标 Excel 文件: {importer.target_file.name}")
|
||||
print(f"VBA 代码目录: {importer.vba_dir}")
|
||||
print(f"模块数量: {len(importer.metadata.get('modules', {}))}")
|
||||
print()
|
||||
print("=" * 60)
|
||||
print()
|
||||
|
||||
success = importer.import_vba()
|
||||
|
||||
print()
|
||||
print("=" * 60)
|
||||
if success:
|
||||
print("导入成功完成!")
|
||||
else:
|
||||
print("导入失败")
|
||||
print("=" * 60)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user