闲社服务运行正常AI智能体自动化平台
d

创建Word表格

技能标识:docx-tables

Create Word documents with properly formatted tables using docx npm library. Tables work consistently across Word and Google Docs. Use when creating DOCX files with tables, especially itineraries, schedules, or data tables.

作者:admin | 来源记录:ClawHub
登记来源
ClawHub
版本
V 2.0.0
检测标记
后台标记通过
免费
免费获取
0
收藏
来源与检测标记为平台登记信息,并不代表已展示可复核的检测报告。使用前请核对版本、依赖和所需权限,建议先在隔离环境中运行。
概述
安装方式
版本历史

创建Word表格

docx-tables

创建可在任何地方正常使用的Word文档表格——Word、Google Docs等。

⚠️ 5条关键规则

1. 双重宽度设置(最关键)

在两个位置设置宽度——表格本身和每个单元格:

javascript
// 表格级别
new Table({
width: { size: 9360, type: WidthType.DXA },
columnWidths: [1872, 7488],
rows: [...]
})

// 单元格级别——每个单元格都需要宽度!
new TableCell({
width: { size: 1872, type: WidthType.DXA },
children: [...]
})

2. 仅使用DXA,绝不使用百分比

百分比在Google Docs中会出错。请使用DXA:

  • - 1英寸 = 1440 DXA
  • 带1英寸页边距的US Letter纸张 = 9360 DXA

javascript
// ❌ 错误
width: { size: 100, type: WidthType.PERCENTAGE }

// ✅ 正确
width: { size: 9360, type: WidthType.DXA }

3. 使用ShadingType.CLEAR

javascript
const { ShadingType } = require(docx);

// ❌ 错误——黑色背景!
shading: { type: ShadingType.SOLID, fill: E0F2F1 }

// ✅ 正确
shading: { type: ShadingType.CLEAR, fill: E0F2F1 }

4. 添加单元格内边距

javascript
const cellMargins = { top: 80, bottom: 80, left: 120, right: 120 };

new TableCell({
margins: cellMargins,
children: [...]
})

5. 列宽必须精确求和

对于1英寸页边距:9360 DXA

javascript
columnWidths: [1872, 7488] // = 9360 ✓
columnWidths: [3120, 3120, 3120] // = 9360 ✓

完整工作示例

javascript
const { Document, Packer, Paragraph, TextRun, Table, TableRow, TableCell,
WidthType, AlignmentType, BorderStyle, TableLayoutType, ShadingType } = require(docx);
const fs = require(fs);

const TOTAL_WIDTH = 9360;
const COL1 = 1872;
const COL2 = 7488;

const cellBorders = {
top: { style: BorderStyle.SINGLE, size: 1, color: CCCCCC },
bottom: { style: BorderStyle.SINGLE, size: 1, color: CCCCCC },
left: { style: BorderStyle.SINGLE, size: 1, color: CCCCCC },
right: { style: BorderStyle.SINGLE, size: 1, color: CCCCCC }
};

const cellMargins = { top: 80, bottom: 80, left: 120, right: 120 };

const doc = new Document({
sections: [{
properties: {
page: { margin: { top: 1440, right: 1440, bottom: 1440, left: 1440 } }
},
children: [
new Table({
layout: TableLayoutType.FIXED,
width: { size: TOTAL_WIDTH, type: WidthType.DXA },
columnWidths: [COL1, COL2],
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph({
children: [new TextRun({ text: Header, bold: true, color: FFFFFF })],
alignment: AlignmentType.CENTER
})],
width: { size: TOTAL_WIDTH, type: WidthType.DXA },
columnSpan: 2,
shading: { type: ShadingType.CLEAR, fill: 1565C0 },
borders: cellBorders,
margins: cellMargins
})
]
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph(Col 1)],
width: { size: COL1, type: WidthType.DXA },
borders: cellBorders,
margins: cellMargins
}),
new TableCell({
children: [new Paragraph(Col 2)],
width: { size: COL2, type: WidthType.DXA },
borders: cellBorders,
margins: cellMargins
})
]
})
]
})
]
}]
});

Packer.toBuffer(doc).then(buffer => {
fs.writeFileSync(output.docx, buffer);
});

列宽速查表

对于带1英寸页边距的US Letter纸张 = 9360 DXA:

布局列宽
2列(20/80)[1872, 7488]
2列(25/75)
[2340, 7020] |
| 2列(等宽) | [4680, 4680] |
| 3列(等宽) | [3120, 3120, 3120] |
| 3列(25/25/50) | [2340, 2340, 4680] |

故障排除

Google Docs中表格变窄?

  • - 使用WidthType.DXA,而非PERCENTAGE
  • 为每个TableCell添加width

单元格背景变黑?

  • - 使用ShadingType.CLEAR,而非SOLID

文字紧贴边框?

  • - 添加margins: { top: 80, bottom: 80, left: 120, right: 120 }

列宽不均匀?

  • - 确认列宽总和精确等于9360

标签

skill ai

通过对话安装

以下为平台配置的接入选项,并非逐项实测通过。能否安装取决于客户端支持、技能来源和运行环境:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 docx-tables-1776351602 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 docx-tables-1776351602 技能

通过命令行安装

skillhub install docx-tables-1776351602

下载

⬇ 下载 docx-tables v2.0.0(免费)

文件大小: 2.18 KB | 发布时间: 2026-4-17 14:40

v2.0.0 最新 2026-4-17 14:40
**Major update: Now uses the docx npm library instead of python-docx, with focus on consistent table rendering across Word and Google Docs.**

- Switched from python-docx to docx (npm) for DOCX table generation.
- Added clear rules for full-width tables that work in both Word and Google Docs: always set widths on both tables and cells, use DXA units only, sum column widths exactly, and use ShadingType.CLEAR.
- Provided concise code examples and a complete usage demo in JavaScript.
- Included troubleshooting advice and width cheat sheet for common layouts.
返回顶部