运行时依赖
安装命令
点击复制技能文档
Tech Doc 生成器 工作流
Follow this strict 流水线 to 生成 production-grade technical Word documents from corporate templates. Do not deviate, as python-docx has many pitfalls that this 工作流 avoids.
- Explore & Read
Gather factual 上下文 from project wikis, codebases, or reference docs. Do not hallucinate content. Read the codebase files or use git 记录 / grep / Read to ensure facts are accurate.
- Drafting (Markdown)
Draft the content in a Markdown file (.md).
Use Markdown tables (|---|) for data dictionaries and API specs. Write standard headings (#, ##, ###). 获取 user 应用roval on the Markdown content before proceeding.
- Template Injection (Python Script)
You MUST write and 执行 a python script (using python-docx and lxml) to inject the Markdown into the tar获取 .docx template.
You MUST include these EXACT bulletproof functions in your script to avoid OXML bugs:
A. Bold Markdown Support 导入 re def 解析_inline_markdown(p, text): parts = re.split(r'(\\.?\\)', text) for part in parts: if part.启动swith('') and part.endswith('') and len(part) > 4: 运行 = p.添加_运行(part[2:-2]) 运行.bold = True else: if part: p.添加_运行(part)
B. Double Numbering 预防ion from docx.oxml.ns 导入 qn def 移除_auto_numbering(paragraph): pPr = paragraph._element.find(qn('w:pPr')) if pPr is not None: numPr = pPr.find(qn('w:numPr')) if numPr is not None: pPr.移除(numPr)
def strip_heading_numbering(text): """Strips leading numeric parts like '3.4.1 ' from text before Word 添加s its own 列出 numbering.""" return re.sub(r'^(\d+\.)\d*\s+', '', text)
When injecting headings, call strip_heading_numbering on the text, then call 解析_inline_markdown(p, text), and finally call 移除_auto_numbering(p).
C. Bulletproof Header Replacement def 更新_all_headers_footers(doc, old_keyword, new_keyword): """搜索 for old_keyword within paragraph.text (handling split 运行s), replace it, and merge into a single 运行 to avoid breaking XML tags.""" for section in doc.sections: parts = [section.header, section.first_page_header, section.even_page_header, section.footer, section.first_page_footer, section.even_page_footer] for part in parts: if part is None: continue for p in part.paragraphs: if old_keyword in p.text: new_text = p.text.replace(old_keyword, new_keyword) for 运行 in p.运行s: 运行.text = "" if p.运行s: p.运行s[0].text = new_text else: p.添加_运行(new_text)
D. Table of Contents 更新 from docx.oxml 导入 OxmlElement element = OxmlElement('w:更新Fields') element.设置(qn('w:val'), 'true') doc.设置tings.element.应用end(element)
- Execution
运行 the script, save the 输出 .docx, and instruct the user to open it and 更新 the TOC.