141 lines
4.5 KiB
Python
141 lines
4.5 KiB
Python
|
|
"""
|
|||
|
|
同步模板和配置文件
|
|||
|
|
扫描模板文件中的占位符,更新配置文件
|
|||
|
|
"""
|
|||
|
|
import json
|
|||
|
|
from pathlib import Path
|
|||
|
|
from template_scanner import scan_docx_placeholders
|
|||
|
|
|
|||
|
|
|
|||
|
|
def sync_template_and_config(category_dir: Path):
|
|||
|
|
"""
|
|||
|
|
同步模板和配置文件
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
category_dir: 配置类型目录
|
|||
|
|
"""
|
|||
|
|
config_path = category_dir / "config.json"
|
|||
|
|
template_path = category_dir / "template.docx"
|
|||
|
|
|
|||
|
|
if not config_path.exists():
|
|||
|
|
print(f" ✗ 配置文件不存在: {config_path}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
if not template_path.exists():
|
|||
|
|
print(f" ✗ 模板文件不存在: {template_path}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
# 读取配置文件
|
|||
|
|
with config_path.open('r', encoding='utf-8') as f:
|
|||
|
|
config = json.load(f)
|
|||
|
|
|
|||
|
|
# 扫描模板文件中的占位符
|
|||
|
|
print(f" 扫描模板: {template_path.name}")
|
|||
|
|
scan_result = scan_docx_placeholders(template_path)
|
|||
|
|
|
|||
|
|
# 合并所有类型的占位符
|
|||
|
|
placeholders = (
|
|||
|
|
scan_result.texts |
|
|||
|
|
scan_result.tables |
|
|||
|
|
scan_result.charts |
|
|||
|
|
scan_result.manual_tables |
|
|||
|
|
scan_result.script_tables |
|
|||
|
|
scan_result.script_charts |
|
|||
|
|
scan_result.db_texts
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
if not placeholders:
|
|||
|
|
print(f" ⚠ 模板中没有找到占位符")
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
print(f" 找到 {len(placeholders)} 个占位符")
|
|||
|
|
|
|||
|
|
# 更新配置文件中的占位符
|
|||
|
|
if 'placeholders' not in config:
|
|||
|
|
config['placeholders'] = {}
|
|||
|
|
|
|||
|
|
# 保留现有占位符的配置,只添加新的
|
|||
|
|
existing_keys = set(config['placeholders'].keys())
|
|||
|
|
template_keys = set(placeholders)
|
|||
|
|
|
|||
|
|
# 新增的占位符
|
|||
|
|
new_keys = template_keys - existing_keys
|
|||
|
|
# 已删除的占位符
|
|||
|
|
removed_keys = existing_keys - template_keys
|
|||
|
|
|
|||
|
|
if new_keys:
|
|||
|
|
print(f" 新增占位符: {', '.join(new_keys)}")
|
|||
|
|
for key in new_keys:
|
|||
|
|
# 根据占位符名称确定类型
|
|||
|
|
if key in scan_result.texts or key in scan_result.db_texts:
|
|||
|
|
ph_type = "text"
|
|||
|
|
elif key in scan_result.tables:
|
|||
|
|
ph_type = "table"
|
|||
|
|
elif key in scan_result.charts:
|
|||
|
|
ph_type = "chart"
|
|||
|
|
elif key in scan_result.manual_tables:
|
|||
|
|
ph_type = "manualTable"
|
|||
|
|
elif key in scan_result.script_tables:
|
|||
|
|
ph_type = "scriptTable"
|
|||
|
|
elif key in scan_result.script_charts:
|
|||
|
|
ph_type = "scriptChart"
|
|||
|
|
else:
|
|||
|
|
ph_type = "text" # 默认类型
|
|||
|
|
|
|||
|
|
config['placeholders'][key] = {
|
|||
|
|
"type": ph_type,
|
|||
|
|
"label": key,
|
|||
|
|
"title": "",
|
|||
|
|
"value": "",
|
|||
|
|
"dbQuery": "",
|
|||
|
|
"chart": {}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if removed_keys:
|
|||
|
|
print(f" ⚠ 模板中已删除的占位符: {', '.join(removed_keys)}")
|
|||
|
|
print(f" (保留在配置中,如需删除请手动编辑)")
|
|||
|
|
|
|||
|
|
# 保存配置文件
|
|||
|
|
with config_path.open('w', encoding='utf-8') as f:
|
|||
|
|
json.dump(config, f, ensure_ascii=False, indent=2)
|
|||
|
|
|
|||
|
|
print(f" ✓ 配置文件已更新")
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f" ✗ 同步失败: {e}")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
|
|||
|
|
def sync_all_categories():
|
|||
|
|
"""同步所有配置类型"""
|
|||
|
|
base_dir = Path(__file__).resolve().parent
|
|||
|
|
config_root = base_dir / "configs"
|
|||
|
|
|
|||
|
|
if not config_root.exists():
|
|||
|
|
print("configs/ 目录不存在")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
print("开始同步模板和配置...")
|
|||
|
|
print("="*50)
|
|||
|
|
|
|||
|
|
for category_dir in config_root.iterdir():
|
|||
|
|
if not category_dir.is_dir():
|
|||
|
|
continue
|
|||
|
|
|
|||
|
|
print(f"\n处理分类: {category_dir.name}")
|
|||
|
|
sync_template_and_config(category_dir)
|
|||
|
|
|
|||
|
|
print("\n" + "="*50)
|
|||
|
|
print("同步完成!")
|
|||
|
|
print("\n提示:")
|
|||
|
|
print("1. 配置文件已根据模板中的占位符更新")
|
|||
|
|
print("2. 新增的占位符已添加到配置中")
|
|||
|
|
print("3. 已删除的占位符保留在配置中(需要手动删除)")
|
|||
|
|
print("4. 请检查配置文件并根据需要调整")
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
sync_all_categories()
|