refactor: migrate configuration to .env file for better environment management
- Replace hardcoded file paths with environment variables in extract_vba.py and import_vba.py - Add .env file for local configuration (excluded from git via .gitignore) - Add python-dotenv dependency for environment variable loading - Maintain fallback to interactive mode when environment variables are not set Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -13,6 +13,14 @@ import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Dict, Any
|
||||
|
||||
# 加载 .env 配置文件
|
||||
try:
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
except ImportError:
|
||||
print("警告: 未安装 python-dotenv 库,将使用默认配置")
|
||||
print("建议运行: pip install python-dotenv")
|
||||
|
||||
try:
|
||||
import win32com.client as win32
|
||||
except ImportError:
|
||||
@@ -21,11 +29,8 @@ except ImportError:
|
||||
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"
|
||||
# 从 .env 文件读取配置,如果未设置则使用 None(默认模式)
|
||||
TARGET_METADATA_FILE = os.getenv("TARGET_METADATA_FILE", "").strip() or None
|
||||
# =================================================
|
||||
|
||||
# 常量定义
|
||||
|
||||
Reference in New Issue
Block a user