# @alex-legal/html-to-docx 中文文档

规范地址：https://npmcn.com/package/%40alex-legal/html-to-docx/

版本：0\.0\.9
npm 元数据更新时间：2026-05-06T12:09:19.292Z
中文译文更新时间：2026-11-06T12:49:27.210Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40alex-legal/html-to-docx
原文对照：https://npmcn.com/package/%40alex-legal/html-to-docx/?lang=original
项目主页：https://github.com/alex-legal/html-to-docx/html-to-docx#readme
源码仓库：https://github.com/alex-legal/html-to-docx

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：4.8 MB，12 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：未声明
- 运行依赖：13 个
- 首次发布：2025-05-19T15:30:41.910Z
- 最新版本发布：2026-05-06T12:09:19.115Z
- 近 30 天下载量：3191（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：9（近 12 个月 5 个）

## 中文简介

HTML 到 DOCX 转换器

## 安装

```sh
npm install @alex-legal/html-to-docx
```

也可以使用：`pnpm add @alex-legal/html-to-docx`、`yarn add @alex-legal/html-to-docx`、`bun add @alex-legal/html-to-docx`

## README

# html- to- docx 数据

[![NPM Version](https://img.shields.io/npm/v/html-to-docx.svg)](https://npmjs.org/package/html-to-docx)

html-to-docx是一个js库,用于将HTML文档转换为由Microsoft Word 2007+,LibreOffice Writer,Google Docs,WPS Writer等支持的DOCX格式.

它的灵感来自 [html- docx-js (中文(简体) )](https://github.com/evidenceprime/html-docx-js "html-docx-js") project 但缓解了生成的文档与Google Docs和libreOffice Writer等不支持的文字处理器不兼容的问题 [缩略语](https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.altchunk?view=openxml-2.8.1 "altchunks") 特性.

html- to- docx 先前用于 [自由主义](https://github.com/jure/node-libtidy "libtidy") 在解析前清理html,但不得不去除,因为由于节点-gyp引起太多依赖性问题.

### 免责声明

虽然有html-to-docx在生产中运行的例子,但请确保它涵盖你通常遇到的所有情况,因为这不是一个完整的解决方案.

目前它并不直接与浏览器相通,但经过了针对React的测试.

## 安装

使用 npm 安装 foobar .

```bash
npm install html-to-docx
```

## 使用量

```js
await HTMLtoDOCX(htmlString, headerHTMLString, documentOptions, footerHTMLString)
```

完整的示例可见于 `example/`

### 参数

-   `htmlString` <[字符串](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")\> clean html string 等同文档内容.
-   `headerHTMLString` <[字符串](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")\> clean html string 等同页眉. 默认为 `<p></p>` 如果页眉旗是 `true`。 。 。 .
-   `documentOptions` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[对象](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")\> 翻译:
    -   `orientation` <"图片"QQ"地貌"定义了文档的总取向. 默认为肖像.
    -   `pageSize` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[对象](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")\> 默认为美国字母肖像取向.
        -   `width` <[数目](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")\> 本节所有页面的页面宽度 [图瓦卢](https://en.wikipedia.org/wiki/Twip "TWIP")。默认为12240。 最高额为31680. 支持在 [像素](https://en.wikipedia.org/wiki/Pixel#:~:text=Pixels%2C%20abbreviated%20as%20%22px%22,what%20screen%20resolution%20views%20it. "pixel"), (中文(简体) ), [厘米](https://en.wikipedia.org/wiki/Centimetre "cm") 或者说 [单位](https://en.wikipedia.org/wiki/Inch "inch")。 。 。 .
        -   `height` <[数目](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")\> 本节所有页面的高度 [图瓦卢](https://en.wikipedia.org/wiki/Twip "TWIP")默认为15840。 最高额为31680. 支持在 [像素](https://en.wikipedia.org/wiki/Pixel#:~:text=Pixels%2C%20abbreviated%20as%20%22px%22,what%20screen%20resolution%20views%20it. "pixel"), (中文(简体) ), [厘米](https://en.wikipedia.org/wiki/Centimetre "cm") 或者说 [单位](https://en.wikipedia.org/wiki/Inch "inch")。 。 。 .
    -   `margins` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[对象](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")\> 翻译:
        -   `top` <[数目](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")\> 主文档文本边距上方与本节所有页面页面上方之间的相距 [图瓦卢](https://en.wikipedia.org/wiki/Twip "TWIP"). 默认为1440. 支持在 [像素](https://en.wikipedia.org/wiki/Pixel#:~:text=Pixels%2C%20abbreviated%20as%20%22px%22,what%20screen%20resolution%20views%20it. "pixel"), (中文(简体) ), [厘米](https://en.wikipedia.org/wiki/Centimetre "cm") 或者说 [单位](https://en.wikipedia.org/wiki/Inch "inch")。 。 。 .
        -   `right` <[数目](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")\> 页面右边缘与此文档文本范围右边缘之间的相距 [图瓦卢](https://en.wikipedia.org/wiki/Twip "TWIP")默认为 1800 。 支持在 [像素](https://en.wikipedia.org/wiki/Pixel#:~:text=Pixels%2C%20abbreviated%20as%20%22px%22,what%20screen%20resolution%20views%20it. "pixel"), (中文(简体) ), [厘米](https://en.wikipedia.org/wiki/Centimetre "cm") 或者说 [单位](https://en.wikipedia.org/wiki/Inch "inch")。 。 。 .
        -   `bottom` <[数目](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")\> 文档文本边距下方与页面下方之间的相距 [图瓦卢](https://en.wikipedia.org/wiki/Twip "TWIP"). 默认为1440. 支持在 [像素](https://en.wikipedia.org/wiki/Pixel#:~:text=Pixels%2C%20abbreviated%20as%20%22px%22,what%20screen%20resolution%20views%20it. "pixel"), (中文(简体) ), [厘米](https://en.wikipedia.org/wiki/Centimetre "cm") 或者说 [单位](https://en.wikipedia.org/wiki/Inch "inch")。 。 。 .
        -   `left` <[数目](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")\> 此文档页面左边缘与文本范围左边缘之间的相距 [图瓦卢](https://en.wikipedia.org/wiki/Twip "TWIP")默认为 1800 。 支持在 [像素](https://en.wikipedia.org/wiki/Pixel#:~:text=Pixels%2C%20abbreviated%20as%20%22px%22,what%20screen%20resolution%20views%20it. "pixel"), (中文(简体) ), [厘米](https://en.wikipedia.org/wiki/Centimetre "cm") 或者说 [单位](https://en.wikipedia.org/wiki/Inch "inch")。 。 。 .
        -   `header` <[数目](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")\> 从页面上边缘到页眉上边缘的相距 [图瓦卢](https://en.wikipedia.org/wiki/Twip "TWIP")默认为 720。 支持在 [像素](https://en.wikipedia.org/wiki/Pixel#:~:text=Pixels%2C%20abbreviated%20as%20%22px%22,what%20screen%20resolution%20views%20it. "pixel"), (中文(简体) ), [厘米](https://en.wikipedia.org/wiki/Centimetre "cm") 或者说 [单位](https://en.wikipedia.org/wiki/Inch "inch")。 。 。 .
        -   `footer` <[数目](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")\> 从页面下边缘到页脚下边缘的距离 [图瓦卢](https://en.wikipedia.org/wiki/Twip "TWIP")默认为 720。 支持在 [像素](https://en.wikipedia.org/wiki/Pixel#:~:text=Pixels%2C%20abbreviated%20as%20%22px%22,what%20screen%20resolution%20views%20it. "pixel"), (中文(简体) ), [厘米](https://en.wikipedia.org/wiki/Centimetre "cm") 或者说 [单位](https://en.wikipedia.org/wiki/Inch "inch")。 。 。 .
        -   `gutter` <[数目](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")\> 超出任何现有比值的指定比值外加的空间量。 此设置通常用于创建文档以装入 [图瓦卢](https://en.wikipedia.org/wiki/Twip "TWIP")。默认为 0。 支持在 [像素](https://en.wikipedia.org/wiki/Pixel#:~:text=Pixels%2C%20abbreviated%20as%20%22px%22,what%20screen%20resolution%20views%20it. "pixel"), (中文(简体) ), [厘米](https://en.wikipedia.org/wiki/Centimetre "cm") 或者说 [单位](https://en.wikipedia.org/wiki/Inch "inch")。 。 。 .
    -   `title` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[字符串](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")\> 文件标题.
    -   `subject` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[字符串](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")\> 文件主题.
    -   `creator` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[字符串](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")\> 文档创建者。 默认为 `html-to-docx`
    -   `keywords` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[阵列](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array")<[字符串](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")QQ 与文档相關的关键字 。 默认为 \['html- to- docx'\].
    -   `description` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[字符串](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")\> 文件说明.
    -   `lastModifiedBy` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[字符串](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")\> 文件的最后修改符。 默认为 `html-to-docx`。 。 。 .
    -   `revision` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[数目](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")\> 文件的修订。 默认为 `1`。 。 。 .
    -   `createdAt` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[日期](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date "Date")\> 创建文档的时间。 默认为当前时间 .
    -   `modifiedAt` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[日期](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date "Date")\> 文件上次修改的时间。 默认为当前时间 .
    -   `headerType` <"default""QQ" 第一位"Even""even""等标题类型. 默认为 `default`。 。 。 .
    -   `header` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[布尔](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")\> 启用标题的旗帜 。 默认为 `false`。 。 。 .
    -   `footerType` <"default""QQ""first""even""等脚型. 默认为 `default`。 。 。 .
    -   `footer` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[布尔](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")\> 启用页脚的旗帜 。 默认为 `false`。 。 。 .
    -   `font` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[字符串](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")\> 要使用的字体名称。 默认为 `Times New Roman`。 。 。 .
    -   `fontSize` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[数目](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")\> HIP(半点)中字体的大小. 默认为 `22`。支持在 [ưμ㼯a](https://en.wikipedia.org/wiki/Point_\(typography\) "pt")。 。 。 .
    -   `complexScriptFontSize` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[数目](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")\> HIP(半点)中复杂脚本字体的大小. 默认为 `22`。支持在 [ưμ㼯a](https://en.wikipedia.org/wiki/Point_\(typography\) "pt")。 。 。 .
    -   `table` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[对象](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")\> 翻译:
        -   `row` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[对象](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")\> 翻译:
            -   `cantSplit` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[布尔](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")\> 旗帜允许表格行分页 。 默认为 `false`。 。 。 .
    -   `pageNumber` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[布尔](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")\> 启用页脚中页码的旗帜。 默认为 `false`。页码只有在页脚旗帜设置为 `true`。 。 。 .
    -   `skipFirstHeaderFooter` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[布尔](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")\> 跳过首页页页眉和页脚的旗帜 。 默认为 `false`。 。 。 .
    -   `lineNumber` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[布尔](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")\> 启用行号的旗帜 。 默认为 `false`。 。 。 .
    -   `lineNumberOptions` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[对象](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")\> 翻译:
        -   `start` <[数目](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")\> 开始编号 - 1. 默认为 `0`。 。 。 .
        -   `countBy` <[数目](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")\> 在 + 1 之间的行数中跳过编号。 默认为 `1`。 。 。 .
        -   `restart` <"持续""QQ""新Page""新科""编号重启策略". 默认为 `continuous`。 。 。 .
    -   `numbering` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[对象](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")\> 翻译:
        -   `defaultOrderedListStyleType` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[字符串](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")\> 默认命令列表样式类型 。 默认为 `decimal`。 。 。 .
    -   `decodeUnicode` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[布尔](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")\> 旗帜可以实现头,身和脚的unicode解码. 默认为 `false`。 。 。 .
    -   `lang` {\\fn华文仿宋\\fs16\\1cHD1D1D1} < ?[字符串](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")\> 拼写检查器语言本地化代码正常工作. 默认为 `en-US`。 。 。 .
-   `footerHTMLString` <[字符串](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")\> clean html 字符串等同页脚. 默认为 `<p></p>` 如果页脚旗是 `true`。 。 。 .

### 回返

<[答应](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise")<[缓冲](https://nodejs.org/api/buffer.html#buffer_buffer "Buffer")|[块](https://developer.mozilla.org/en-US/docs/Web/API/Blob "Blob")\>>

## 页:1

目前,尽管"页面断开-后"的值,但可以通过有类名"页面断开"或样式"页面断开-后"的div来进行分页,div元素内部的内容会被忽略. `<div class="page-break" style="page-break-after: always;"></div>`

CSS 列表样式类型 `<ol>` 现在支持元素。 在HTML中做这样的事情:

```
  <ol style="list-style-type:lower-alpha;">
    <li>List item</li>
    ...
  </ol>
```

所支持的列表类型列表 :

-   上阿尔法,将导致 `A. List item`
-   下阿尔法,将导致 `a. List item`
-   上罗马语,将导致 `I. List item`
-   下罗马语,将导致 `i. List item`
-   下阿尔法-括号端,将导致 `a) List item`
-   小数点后端,将产生 `1) List item`
-   小数括号,将导致 `(1) List item`
-   小数, **(默认)** 将会导致 `1. List item`

也可以添加属性 `data-start="n"` 从 n-th 开始编号.

`<ol data-start="2">` 将从(B.b. 二. 二. 2.)开始编号

字体家族并不对所有单词处理器软件一致工作

-   Word 桌面工作如意
-   LibreOffice 忽略了字体Table.xml文件,并自己找到字体
-   Word Online 忽略了字体Table.xml文件,并在其字体库中找到最接近的字体

## 捐款

欢迎拉动请求。 对于重大变动,请先开出一个问题来讨论你想要改变什么.

请确保将新的分支机构从开发中分离出来,以作出贡献.

## 支助

[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/privateOmega)

## 许可证

麻省理工学院

## 贡献者

[![](https://contrib.rocks/image?repo=privateomega/html-to-docx)](https://github.com/privateomega/html-to-docx/graphs/contributors)

制造的 [缩写. rocks](https://contrib.rocks/)。 。 。 .

## 常见问题

### @alex-legal/html-to-docx 是什么？

HTML 到 DOCX 转换器

### 如何安装 @alex-legal/html-to-docx？

在项目目录运行 npm install @alex-legal/html-to-docx，或使用 pnpm add / yarn add / bun add。

### @alex-legal/html-to-docx 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/alex-legal\_\_html-to-docx 是否存在。

### @alex-legal/html-to-docx 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @alex-legal/html-to-docx 可以商用吗？

@alex-legal/html-to-docx 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @alex-legal/html-to-docx 的下载量有多少？

根据 npm 官方下载统计，@alex-legal/html-to-docx 在 2026/10/06 – 2026/11/04 这 30 天内被下载 3,191 次，日均约 106 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @alex-legal/html-to-docx 还在维护吗？

最新版本 0\.0\.9 发布于 2026/05/06（6 个月前）；至今共发布 9 个版本，其中过去 12 个月发布了 5 个；首次发布于 2025/05/19。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

