add: enhance TARGET_XLSM_FILE configuration and improve file selection process in extract_vba.py
This commit is contained in:
@@ -11,6 +11,13 @@ import re
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Dict, List, Tuple, Optional
|
from typing import Dict, List, Tuple, Optional
|
||||||
|
|
||||||
|
# ==================== 配置区域 ====================
|
||||||
|
# 在此配置要处理的xlsm文件路径
|
||||||
|
# 支持相对路径(相对于脚本所在目录)或绝对路径
|
||||||
|
# 留空或设为None则使用交互模式(自动查找Excel文件夹)
|
||||||
|
TARGET_XLSM_FILE = r"C:\Users\Administrator\Desktop\新BOM\AutoBOM\YTHN-100.A0.532-BOM-1.2版 Claude.xlsm" # 例如: r"Excel\myfile.xlsm" 或 r"D:\path\to\file.xlsm"
|
||||||
|
# =================================================
|
||||||
|
|
||||||
# VBA项目相关常量
|
# VBA项目相关常量
|
||||||
STANDARD_MODULE_DIR = "Modules"
|
STANDARD_MODULE_DIR = "Modules"
|
||||||
CLASS_MODULE_DIR = "ClassModules"
|
CLASS_MODULE_DIR = "ClassModules"
|
||||||
@@ -22,16 +29,21 @@ METADATA_FILE = "vba_metadata.json"
|
|||||||
class VBAExtractor:
|
class VBAExtractor:
|
||||||
"""VBA代码提取器"""
|
"""VBA代码提取器"""
|
||||||
|
|
||||||
def __init__(self, xlsm_path: str, output_dir: str = None):
|
def __init__(self, xlsm_path: str, output_dir: str = None, use_same_dir: bool = True):
|
||||||
"""
|
"""
|
||||||
初始化VBA提取器
|
初始化VBA提取器
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
xlsm_path: xlsm文件路径
|
xlsm_path: xlsm文件路径
|
||||||
output_dir: 输出目录,默认为项目根目录下的VBA文件夹
|
output_dir: 输出目录(当use_same_dir=False时有效)
|
||||||
|
use_same_dir: 是否使用目标文件同目录下的VBA文件夹,默认True
|
||||||
"""
|
"""
|
||||||
self.xlsm_path = Path(xlsm_path)
|
self.xlsm_path = Path(xlsm_path)
|
||||||
if output_dir is None:
|
|
||||||
|
if use_same_dir:
|
||||||
|
# 使用目标文件同目录下的VBA文件夹
|
||||||
|
self.output_dir = self.xlsm_path.parent / "VBA"
|
||||||
|
elif output_dir is None:
|
||||||
# 使用脚本所在目录(项目根目录)下的VBA文件夹
|
# 使用脚本所在目录(项目根目录)下的VBA文件夹
|
||||||
script_dir = Path(__file__).parent
|
script_dir = Path(__file__).parent
|
||||||
self.output_dir = script_dir / "VBA"
|
self.output_dir = script_dir / "VBA"
|
||||||
@@ -339,7 +351,28 @@ def main():
|
|||||||
print("=" * 60)
|
print("=" * 60)
|
||||||
print()
|
print()
|
||||||
|
|
||||||
# 查找xlsm文件
|
# 检查是否配置了目标文件
|
||||||
|
if TARGET_XLSM_FILE and TARGET_XLSM_FILE.strip():
|
||||||
|
# 使用配置的文件路径
|
||||||
|
script_dir = Path(__file__).parent
|
||||||
|
target_path = Path(TARGET_XLSM_FILE)
|
||||||
|
|
||||||
|
# 如果是相对路径,则相对于脚本所在目录
|
||||||
|
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.suffix.lower() == '.xlsm':
|
||||||
|
print(f"警告: 文件扩展名不是.xlsm: {target_path.name}")
|
||||||
|
|
||||||
|
xlsm_file = target_path
|
||||||
|
print(f"使用配置文件: {xlsm_file.name}")
|
||||||
|
print()
|
||||||
|
else:
|
||||||
|
# 交互模式:查找xlsm文件
|
||||||
excel_dir = Path("Excel")
|
excel_dir = Path("Excel")
|
||||||
if not excel_dir.exists():
|
if not excel_dir.exists():
|
||||||
print("错误: 未找到Excel文件夹")
|
print("错误: 未找到Excel文件夹")
|
||||||
@@ -373,9 +406,13 @@ def main():
|
|||||||
print(f"选择文件: {xlsm_file.name}")
|
print(f"选择文件: {xlsm_file.name}")
|
||||||
print()
|
print()
|
||||||
|
|
||||||
# 创建提取器
|
# 创建提取器(使用默认参数,输出到目标文件同目录下的VBA文件夹)
|
||||||
extractor = VBAExtractor(str(xlsm_file))
|
extractor = VBAExtractor(str(xlsm_file))
|
||||||
|
|
||||||
|
# 显示输出目录信息
|
||||||
|
print(f"输出目录: {extractor.output_dir}")
|
||||||
|
print()
|
||||||
|
|
||||||
# 选择提取方法
|
# 选择提取方法
|
||||||
print("请选择提取方法:")
|
print("请选择提取方法:")
|
||||||
print(" 1. COM接口 (推荐 - 需要安装Excel)")
|
print(" 1. COM接口 (推荐 - 需要安装Excel)")
|
||||||
|
|||||||
Reference in New Issue
Block a user