📌 重要備忘 #30

🔄 merge-docx 優化實施計劃

📅 2026-04-09 ⏱️ 10:53

merge-docx .wb-config.json 多專案通用 upload-html updatememo

📋 背景:教學工作流位置

1
ideatorich — 構想收集
主公說「教學構想:...」→ 由貞寫入 ideatorich.docx
2
主公審核 — 審核構想
3
merge-docx(本次優化) — 合併更新 docx
將 ideatorich.docx 的構想段落合併進 aiworkforme.docx
4
主公審核 — 最終確認
5
content-studio — HTML 預覽
6
aiworkforme-update — 部署上線

❌ 現況問題(v1.0.0)

Hardcode 路徑導致的限制

🔍 參考:可配置通用技能的模式

技能配置方式自動偵測版本
upload-html .wb-config.json ✅ 自動搜尋 ./ ../ ../.. v3.0
updatememo .wb-config.json ✅ 自動搜尋 ./ ../ ../.. v2.0
updatesk .wb-config.json ✅ 自動搜尋 ./ ../ ../.. v2.0
merge-docx ❌ Hardcode ❌ 需手動指定 v1.0

✅ 優化方案(v2.0.0)

三大核心改進

📁 新配置檔格式

欄位說明預設值
project 專案名稱 aiworkforme
docx.source 構想來源檔(ideatorich.docx) ideatorich.docx
docx.target 目標主檔(aiworkforme.docx) aiworkforme.docx
docx.outputDir 輸出目錄 同 docx.source 位置
docx.outputPrefix 輸出檔前綴 aiworkforme_v

配置檔範例

{
  "project": "aiworkforme",
  "docx": {
    "source": "ideatorich.docx",
    "target": "aiworkforme.docx",
    "outputDir": "C:\\Users\\Administrator\\WorkBuddy\\工作備忘",
    "outputPrefix": "aiworkforme_v"
  }
}

主公(課程備課)配置

{
  "project": "aiworkforme",
  "docx": {
    "source": "ideatorich.docx",
    "target": "aiworkforme.docx",
    "outputDir": "C:\\Users\\Administrator\\WorkBuddy\\工作備忘"
  }
}

學員(練習用)配置

{
  "project": "aiworkforme-student",
  "docx": {
    "source": "my-ideas.docx",
    "target": "aiworkforme-練習.docx",
    "outputDir": "C:\\Users\\Administrator\\Documents\\AiWorkForMe"
  }
}

🔍 自動偵測邏輯

1
偵測配置檔
執行時自動搜尋 .wb-config.json
偵測順序:
1. ./wb-config.json(當前目錄)
2. ../wb-config.json(上一層)
3. ../../wb-config.json(上兩層)
4. 找到即停
2
推導檔案路徑
根據配置檔位置自動計算完整路徑
3
執行合併
使用 Python + python-docx 合併文檔
4
版本化輸出
生成 aiworkforme_v{timestamp}.docx

📊 升級對比:v1.0 vs v2.0

❌ v1.0(現況)

✅ v2.0(優化後)

🔄 新觸發方式

觸發方式說明自動化程度
「執行 merge-docx」 直接執行,使用配置檔自動偵測 ★★★ 完全自動
「合併構想進文檔」 自然語言觸發 ★★★ 完全自動
「合併到 aiworkforme」 手動指定目標 ★ 需確認

📋 實施清單

⚡ merge-docx v2.0 建置清單

🛠️ 技術實作方向

Python 合併核心邏輯

import docx
import os
import json
from datetime import datetime

def find_config():
    """自動偵測 .wb-config.json"""
    search_paths = ['.', '..', '../..']
    for path in search_paths:
        config_path = os.path.join(path, '.wb-config.json')
        if os.path.exists(config_path):
            with open(config_path, 'r', encoding='utf-8') as f:
                return json.load(f)
    return None  # 使用預設值

def merge_docx(config):
    """合併 docx"""
    # 讀取配置
    source = config['docx']['source']
    target = config['docx']['target']
    output_dir = config['docx']['outputDir']
    
    # 讀取源文檔(ideatorich)
    source_doc = docx.Document(source)
    
    # 讀取目標文檔(aiworkforme)
    target_doc = docx.Document(target)
    
    # 分析源文檔結構
    sections = parse_sections(source_doc)
    
    # 合併到目標
    changes = merge_to_target(target_doc, sections)
    
    # 版本化輸出
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    output_file = f"{config['docx']['outputPrefix']}{timestamp}.docx"
    output_path = os.path.join(output_dir, output_file)
    
    target_doc.save(output_path)
    return output_path, changes

def parse_sections(doc):
    """解析章節結構"""
    sections = []
    current = None
    content = []
    for para in doc.paragraphs:
        if para.style.name.startswith('Heading'):
            if current:
                sections.append({'title': current, 'content': content})
            current = para.text
            content = []
        elif current:
            content.append(para.text)
    if current:
        sections.append({'title': current, 'content': content})
    return sections

def merge_to_target(doc, sections):
    """合併章節到目標文檔"""
    changes = []
    for section in sections:
        # 找對應位置
        pos = find_section_position(doc, section['title'])
        if pos:
            changes.append(f"替換:{section['title']}")
        else:
            changes.append(f"新增:{section['title']}")
        # 執行合併(新增或替換)
    return changes