# DOCX工具

创建、编辑、校验和转换 Word DOCX，支持把完整 LaTeX 论文转换为 DOCX，以及数学建模论文模板、原生公式、三线表、修订和批注。

- Repository: https://github.com/XiaoMaColtAI/math-modeling-skill (1892 stars)
- Category: Documents
- License: see repository
- Last updated: 2026-11-01
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add XiaoMaColtAI/math-modeling-skill --skill DOCX工具 -g -a claude-code -y`
Codex: `npx skills add XiaoMaColtAI/math-modeling-skill --skill DOCX工具 -g -a codex -y`
Cursor: `npx skills add XiaoMaColtAI/math-modeling-skill --skill DOCX工具 -g -a cursor -y`
Gemini CLI: `npx skills add XiaoMaColtAI/math-modeling-skill --skill DOCX工具 -g -a gemini-cli -y`

## Description

创建、编辑、校验和转换 Word DOCX，支持把完整 LaTeX 论文转换为 DOCX，以及数学建模论文模板、原生公式、三线表、修订和批注。

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# DOCX 工具

## 路径与写入

- 当前目录为本工具根目录，只读。
- 模板和脚本从本目录读取。
- 生成或修改后的 DOCX 必须写入用户 `PROJECT_ROOT`。
- 默认不覆盖输入文件或 Skill 文件。

## 数学建模论文推荐流程

采用“当届官方参考模板 + `python-docx` 构建 + OMML 公式 + OOXML 校验 + 渲染抽检”。官方模板控制页面、样式、分节、页眉页脚和编号；代码负责稳定写入内容。

```python
from pathlib import Path
import sys

scripts = Path("<SKILL_ROOT>") / "tools" / "docx" / "scripts"
sys.path.insert(0, str(scripts))
import paper_format as pf

doc = pf.new_document(
    contest="cumcm",
    template_path=Path("<PROJECT_ROOT>") / "当届官方模板.docx",
    preserve_template_content=False,
)
# 此示例只借用模板样式后追加正文。
# 若官方模板包含固定摘要页或编号页，应改为 True 并在原位置填充。
pf.title(doc, "论文题目")
pf.abstract_title(doc)
pf.body(doc, "摘要正文。")
pf.keywords(doc, "优化；预测")
pf.equation(doc, r"\min f(x)=\sum_{i=1}^{n}x_i^2")
pf.three_line_table(doc, [["符号", "说明"], ["x", "决策变量"]])
pf.save_document(doc, Path("<PROJECT_ROOT>"), contest="cumcm")
```

## 公式

### 直接写入

`scripts/equations.py` 把常用 LaTeX 子集转成 Word 原生 OMML。未知命令、未闭合分组和不支持环境会报错，不会静默生成错误文本。

```powershell
python scripts/equations.py replace "输入.docx" `
  --replace "EQ_OBJECTIVE" "\min f(x)=\sum_{i=1}^{n}x_i^2" `
  --output "<PROJECT_ROOT>/输出.docx"
```

同一占位符出现多次时会全部替换。支持分式、上下标、根式、n 次根、常用希腊字母与关系符号、反三角函数和常见矩阵，包括 `\nu`、`\mu`、`\approx`、`\arcsin`、`\arccos`、`\arctan`。

### 复杂公式

复杂 LaTeX 优先使用 Pandoc 的成熟转换：

```powershell
python scripts/equations.py generate "论文.md" `
  --output "<PROJECT_ROOT>/论文.docx" `
  --template "官方模板.docx"
```

转换后仍须校验和渲染抽检。

## 完整 LaTeX 论文转 DOCX

此功能需要预先安装 [Pandoc](https://pandoc.org/installing.html)，并保证 `pandoc` 命令可用；可先运行 `python scripts/check_env.py` 检查。工具将 `.tex` 主入口转换为 DOCX，从 LaTeX 项目目录读取相对图片，使用 citeproc 处理 BibTeX 引用，并把公式写为可编辑 OMML。传入当届官方 DOCX 作为参考模板，以继承 Word 样式和页面设置：

```powershell
python scripts/equations.py convert-latex `
  "<PROJECT_ROOT>/完整论文-LaTeX/main.tex" `
  --output "<PROJECT_ROOT>/完整论文.docx" `
  --template "<PROJECT_ROOT>/当届官方模板.docx" `
  --timeout 120
```

The rest: https://github.com/XiaoMaColtAI/math-modeling-skill/blob/HEAD/dsh-plugin/math-modeling-agent/skills/math-modeling/tools/docx/SKILL.md
