Html To Docx logo

Html To Docx

Community
crane-in-clear-sky
html_to_docx

| 将 HTML 字符串或文件高保真转换为 Microsoft Word (.docx) 文档。 支持 CSS 变量预处理、10+ 种 HTML 元素精确映射、14 种 CSS 属性映射、中文字体、4 种装饰组件、TOC 目录、页眉页脚、图片嵌入。 提供 CLI(subprocess)和 Python API 双模式调用;在需要将 HTML 文档转为可编辑 Word 文件时使用此 skill。

Overview

Publishercrane-in-clear-sky
RepositoryWorkWit-AI-Agent
Skill namehtml_to_docx
Stars
118
Forks
5
Bundled files
Instructions only
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • Self-contained

    Everything the model needs lives in the instructions — no extra files to sync.

  • Open source

    Published by crane-in-clear-sky on GitHub. Read the source before you install it.

Installation

Install the Html To Docx AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/crane-in-clear-sky/WorkWit-AI-Agent/tree/main/workwit/backend/builtin_skills/html_to_docx
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/crane-in-clear-sky/WorkWit-AI-Agent.git /tmp/WorkWit-AI-Agent
mkdir -p .claude/skills
cp -r /tmp/WorkWit-AI-Agent/workwit/backend/builtin_skills/html_to_docx .claude/skills/html_to_docx
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Html To Docx in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Html To Docx on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Html To Docx is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

⚠️ 环境适配:本技能原文用本地 CLI/subprocess 转 HTML→DOCX,当前系统无该 CLI,改用沙箱 run_temp_code(python-docx)完成转换,或直接 generate_word 生成。

html-to-docx Skill

概述

html-to-docx 是一个 Python 转换引擎,支持:

  • CSS 变量(var(--x))预处理
  • 10+ 种 HTML 元素精确映射(标题/段落/表格/列表/图片/blockquote 等)
  • 14 种 CSS 属性映射(字体/颜色/缩进/行距/对齐等)
  • 中文字体支持(宋体/黑体/仿宋/楷体,含 rFonts.eastAsia 设置)
  • 4 种装饰组件(callout / divider / section-marker / data-card)+ data-card-grid
  • 语义 <section role> 分节 + CSS @page 页眉页脚 + 动态域(PAGE/NUMPAGES/STYLEREF)
  • TOC 目录渲染(3 级)
  • 页眉/页脚渲染
  • 图片嵌入(base64 / 本地路径 / 远程 URL)
  • CLI 和 Python API 双模式调用

调用方式

前置条件:Python 环境与依赖安装

本 Skill 使用 托管 venv(默认 $HOME/.venv-html-to-docx)运行,避免污染系统 Python,也绕开 IDE safe-delete 拦截与跨机器 venv 二进制损坏。环境准备已封装为幂等脚本,首次 ~30s、之后 ~0.5s,agent 无需自行 python -m venv / pip install。

⚠️ 首次调用需要外网(幂等脚本首跑会连以下三处公网):

  • https://astral.sh/uv/install.sh — 装 uv(若本机未装)
  • astral-sh/python-build-standalone GitHub Releases — 拉 Python 3.12 独立发行版(uv 托管)
  • https://pypi.org/simple — 下载 python-docx / lxml / Pillow 等 wheel

私有化 / 无外网环境:请在首次调用前导出以下环境变量指向内网镜像(uv 与 pip 均遵循):

bash
export UV_INDEX_URL="https://<内网 PyPI 镜像>/simple"
export UV_PYTHON_INSTALL_MIRROR="https://<内网 python-build-standalone 镜像>"
# uv 本身请预先由运维分发到 $HOME/.local/bin/uv,脚本探测到已装则跳过 curl 安装

未配置镜像且无外网时,setup-html-to-docx.sh 首跑必然失败——这是已知交付前置条件,非 skill 缺陷。Agent 在私有化交付场景下调用本 skill 前,应先确认 IOA/运维已完成上述环境变量或离线 wheel 分发,否则应转 Markdown 降级(见"错误处理与降级")而非重试。

统一入口(推荐,workbuddy / local 通道一键装齐):

单独准备本 Skill 的环境:

说明:本 skill 的环境安装脚本已从 <skill_root>/scripts/setup_env.sh 挪到

<workspace>/src/scripts/wb/local/setup-html-to-docx.sh(workbuddy 本地通道专属)。

plugin 打包后位于 <plugin_root>/scripts/wb/local/setup-html-to-docx.sh。

脚本输出末尾会打印 Python runner: <venv>/bin/python,可直接 export HTML_TO_DOCX_PY=<那个路径> 供后续调用。

调用(首选:环境变量方式):

调用(备选:一行式,用于 subprocess):

Agent 集成提示:

  • setup-html-to-docx.sh 是幂等的,可以每次调用前无脑跑一遍,已就绪时秒退。
  • cwd 需要设为 <skill_root>/scripts(html_to_docx 包在此),或用 PYTHONPATH=<skill_root>/scripts。
  • 需要把 venv 落到别处时,调用前 export HTML_TO_DOCX_VENV=/自定义/路径 即可。
  • 禁止在 <skill_root>/scripts/ 下自建 .venv/——那是过去踩过的坑(跨机器 shebang 失效、rm 被 safe-delete 拦)。
  • Plugin 部署态下,SessionStart hook 已经自动跑过一次安装脚本,agent 调用时通常无需再手动 bash。

依赖清单:以 <skill_root>/scripts/requirements.txt 为准,setup-html-to-docx.sh 用 --only-binary=:all: 强制装 wheel,规避 lxml 在无 libxml2/libxslt 环境下源码构建失败。

Python 版本要求:Python 3.12(由 setup-html-to-docx.sh 通过 uv 自动安装并锁定)。


CLI(Subprocess 模式)

路径定位:html_to_docx 包位于本 Skill 的 scripts/ 目录下。必须使用 setup-html-to-docx.sh 输出的托管 venv 解释器(下文简写为 $HTML_TO_DOCX_PY),并把工作目录切换到 scripts/ 目录,或通过 PYTHONPATH 指定:

其中 <skill_root> 为本 Skill 的绝对路径,即:

Agent 集成提示:在 subprocess 调用时,推荐使用 cwd = path.join(skillRoot, "scripts") + executable = $HTML_TO_DOCX_PY,无需设置 PYTHONPATH。不要使用系统 python / python3——依赖装在托管 venv 里,系统解释器会 ModuleNotFoundError。

参数:

参数默认值说明
input_path(必须)输入 HTML 文件路径
--output / -o自动生成输出 .docx 文件路径
--page-sizeA4A4 / Letter / A3
--orientationportraitportrait / landscape
--margin-top2.54上边距(cm)
--margin-bottom2.54下边距(cm)
--margin-left3.17左边距(cm)
--margin-right3.17右边距(cm)

退出码:

  • 0 — 成功,stdout 输出 JSON ConvertResult
  • 1 — 失败,stderr 输出 JSON 错误信息

输出示例(成功):

输出示例(失败):

Python API 模式


输入契约

HTML 字符串规范

支持:

  • 标准 HTML5 结构(<body> 内容)
  • CSS 变量(<style>:root { --color: red; }</style>)
  • 特殊区域标签:
    • <header class="doc-header"> — 文档页眉
    • <footer class="doc-footer"> — 文档页脚
    • <nav class="doc-toc"> — 目录
    • <div data-component="callout|divider|section-marker|data-card"> — 装饰组件
    • <section role="..."> — 语义分节(自动映射 docx section,详见下)
    • <style>@page { ... }</style> — CSS Paged Media 页眉页脚模型(详见下)
    • <div class="data-card-grid"> — 卡片网格容器
    • <span data-docx-field="party_a" data-placeholder="请输入甲方名称"></span> — 行内待填写字段,转换后写为 DOCX 书签
待填写字段与 DOCX 书签

对合同、报价单等需要后续精确填写的位置,使用行内 span:

  • data-docx-field 必填,须在同一 HTML 中唯一,并匹配

    ^[A-Za-z][A-Za-z0-9_-]{0,63}$。

  • data-placeholder 可选;未提供时显示默认待填写提示。

  • 仅支持同一段落或表格单元格内的行内字段;不支持跨段落、页眉页脚或重复行字段。

  • 成功转换的 ConvertResult.fields 和 CLI 成功 JSON 会返回字段键与实际 bookmark_name 映射。后续编辑必须按该书签定位;字段缺失时不得回退为全文文本替换。

语义 Section 与 CSS @page 页面模型

1. 语义分节 <section>

<body> 下的顶层 <section> 按 DOM 顺序映射为独立 docx section,节边界自动插入分节符(nextPage)。无 <section> 时按单节处理(向后兼容)。

属性说明默认值
role开放字符串;用于 section[role=X]{page:Y} 命名页绑定无
data-orientationportrait / landscape(尺寸取文档默认并交换 w/h)文档默认
data-margin-top/bottom/left/right该节页边距(cm),per-property 合并,未设项回落文档默认文档默认
data-page-restart该节页码起始值(写入 pgNumType@w:start)不重起

嵌套 <section> 仅顶层计为节,内层降级为普通块 + warning。请把封面/正文内的语义块用 <div> 表达。

2. CSS @page 页眉页脚(CSS Paged Media 有界子集)

在 <style> 中用 @page 声明 6 个 margin-box(@top-left/center/right、@bottom-left/center/right)的页眉页脚内容;命名页 @page <name> + section[role=X]{page:Y} 实现"封面无、正文有"。

content 取值渲染为说明
counter(page)Word PAGE 域当前页码
counter(pages)Word NUMPAGES 域总页数(原生物理总数,不减一)
string(<name>)STYLEREF <styleId> 域配合 string-set,引用样式 styleId(如裸 h1→Heading1,locale 无关)
"文本" / '文本'普通文本字符串字面量
none不产内容某节相关 box 全 none → 真正无页眉/页脚(封面)

域写入后自动置位 <w:updateFields val="true"/>,Word 打开/按 F9 即刷新。

同区域多个 box 合并进单段落,用 Tab 制表位(左/中/右)定位;连续节共用同一 @page 时继承同一 header/footer part(横向节也继承、不重算制表位)。

边界(Python 解析有界子集 C-CON-001):仅支持上述子集;不支持的语法(如 @page :first 伪类、@page { size: ... })→ 忽略 + warning,绝不中断转换。content: ""(空字符串)非法 → 等同未设置 + warning(沿用上一节继承)。

ConvertOptions 参数


输出契约


错误处理与降级

场景行为
整体转换失败success=False + error + markdown_fallback(bs4 提取文本转 Markdown)
单个组件渲染失败跳过该组件 + 追加 warnings,不中断整体转换
图片下载超时(5s)插入占位段落 [图片: URL]
图片文件不存在插入占位段落 [图片无法加载]
未知 data-component跳过 + 追加警告

性能

文档规模P95 目标实测参考
10 页(典型)≤ 3000ms~87ms

组件扩展

添加新组件只需:

  1. 创建 html_to_docx/components/<name>.py
  2. 使用 @register("<component-type>") 装饰渲染函数
  3. 无需修改其他文件(自动发现机制)

调用方式:<div data-component="my-widget">内容</div>


已知限制

  • 复杂嵌套 colspan/rowspan 表格可能有偏差
  • CSS background-color 仅部分支持(需 python-docx 表格单元格)
  • 远程图片依赖网络,超时(5s)时降级为占位文本
  • Word 2007 以下格式不支持

Frequently asked questions

What does the Html To Docx AI skill do?

| 将 HTML 字符串或文件高保真转换为 Microsoft Word (.docx) 文档。 支持 CSS 变量预处理、10+ 种 HTML 元素精确映射、14 种 CSS 属性映射、中文字体、4 种装饰组件、TOC 目录、页眉页脚、图片嵌入。 提供 CLI(subprocess)和 Python API 双模式调用;在需要将 HTML 文档转为可编辑 Word 文件时使用此 skill。

Why use Html To Docx on TypingMind?

Because you install it once and use it with any model. Html To Docx is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Html To Docx in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/crane-in-clear-sky/WorkWit-AI-Agent/tree/main/workwit/backend/builtin_skills/html_to_docx. TypingMind reads its SKILL.md and installs it as a skill you can enable per chat.

Which AI models can use Html To Docx?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Html To Docx?

As many as you like. As long as a model supports skills, you can use Html To Docx with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Html To Docx AI skill free?

It is published on GitHub by crane-in-clear-sky. Check the repository for licensing terms. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇