返回顶部
d

docx-tables创建Word表格

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
安全检测
已通过
318
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

docx-tables

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.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部