返回顶部
p

pretty-tables美化表格

Create beautiful, full-width tables in Word documents using the docx npm library. Tables are properly sized, centered, and styled. Use when you need to create DOCX files with professional-looking tables like itineraries, schedules, data tables, or reports.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 2.0.0
安全检测
已通过
168
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

pretty-tables

pretty-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
  • 美国信纸(8.5英寸)带1英寸页边距 = 9360 DXA内容宽度

javascript
// ❌ 错误 - 在Google Docs中出错
width: { size: 100, type: WidthType.PERCENTAGE }

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

3. 使用ShadingType.CLEAR,而非SOLID

这是一个微妙但关键的陷阱:

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({
children: [...],
margins: cellMargins
})

5. 列宽必须精确求和

对于带1英寸页边距的美国信纸:9360 DXA

javascript
// 2列:20% + 80%
columnWidths: [1872, 7488] // = 9360 ✓

// 3列:等宽
columnWidths: [3120, 3120, 3120] // = 9360 ✓

// 3列:25% + 25% + 50%
columnWidths: [2340, 2340, 4680] // = 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; // 带1英寸页边距的美国信纸
const TIME_COL = 1872; // 20%
const CONTENT_COL = 7488; // 80%

// 浅灰色边框
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 };

// 表格辅助函数
function table(columnWidths, rows) {
return new Table({
layout: TableLayoutType.FIXED,
width: { size: columnWidths.reduce((a, b) => a + b, 0), type: WidthType.DXA },
columnWidths: columnWidths,
rows: rows
});
}

// 表头行
function headerRow(text, color) {
return new TableRow({
children: [
new TableCell({
children: [new Paragraph({
children: [new TextRun({ text, bold: true, size: 32, color: FFFFFF })],
alignment: AlignmentType.CENTER
})],
width: { size: TIMECOL + CONTENTCOL, type: WidthType.DXA },
columnSpan: 2,
shading: { type: ShadingType.CLEAR, fill: color },
borders: cellBorders,
margins: cellMargins
})
]
});
}

// 数据行
function dataRow(time, activity, color) {
return new TableRow({
children: [
new TableCell({
children: [new Paragraph({
children: [new TextRun({ text: time, bold: true, size: 26, color })],
alignment: AlignmentType.CENTER
})],
width: { size: TIME_COL, type: WidthType.DXA },
borders: cellBorders,
margins: cellMargins
}),
new TableCell({
children: [new Paragraph({
children: [new TextRun({ text: activity, size: 26 })]
})],
width: { size: CONTENT_COL, type: WidthType.DXA },
borders: cellBorders,
margins: cellMargins
})
]
});
}

// 构建文档
const doc = new Document({
sections: [{
properties: {
page: { margin: { top: 1440, right: 1440, bottom: 1440, left: 1440 } }
},
children: [
table([TIMECOL, CONTENTCOL], [
headerRow(日程安排, 1565C0),
dataRow(9:00 AM, 到达目的地, 1565C0),
dataRow(10:00 AM, 上午活动, 1565C0),
dataRow(12:00 PM, 午餐休息, 1565C0),
])
]
}]
});

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

列宽速查表

对于美国信纸(8.5英寸)带1英寸页边距 = 9360 DXA

布局列宽总和
2列(20/80)[1872, 7488]9360
2列(25/75)
[2340, 7020] | 9360 |
| 2列(30/70) | [2808, 6552] | 9360 |
| 2列(等宽) | [4680, 4680] | 9360 |
| 3列(等宽) | [3120, 3120, 3120] | 9360 |
| 3列(25/25/50) | [2340, 2340, 4680] | 9360 |
| 3列(20/30/50) | [1872, 2808, 4680] | 9360 |
| 4列(等宽) | [2340, 2340, 2340, 2340] | 9360 |

常见错误

缺少单元格宽度:
javascript
new TableCell({ children: [...] }) // 没有宽度!

使用百分比:
javascript
width: { size: 100, type: WidthType.PERCENTAGE } // 在Google Docs中出错

错误的底纹类型:
javascript
shading: { type: ShadingType.SOLID, fill: E0F2F1 } // 黑色背景!

列宽不匹配:
javascript
columnWidths: [2000, 7000] // = 9000,不是9360

正确:
javascript
new TableCell({
width: { size: 1872, type: WidthType.DXA },
shading: { type: ShadingType.CLEAR, fill: E0F2F1 },
margins: { top: 80, bottom: 80, left: 120, right: 120 },
borders: cellBorders,
children: [...]
})

调色板

表头颜色:
| 颜色 | 十六进制 | 用途 |
|-------|-----|-----

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 pretty-tables-1776289442 技能

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

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

通过命令行安装

skillhub install pretty-tables-1776289442

下载

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

文件大小: 3.35 KB | 发布时间: 2026-4-17 15:48

v2.0.0 最新 2026-4-17 15:48
**Major update for universal DOCX table compatibility across Word and Google Docs:**

- Requires setting widths in DXA units for both the table and every individual cell.
- Abandons percentage widths entirely for full cross-platform reliability.
- Uses `ShadingType.CLEAR` (not `SOLID`) to ensure cell background colors render correctly everywhere.
- Emphasizes proper cell padding (margins) to improve readability and appearance.
- Introduces updated examples and cheat sheets reflecting the best practices and new requirements.
- Adds troubleshooting guides for common rendering issues.

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

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

p2p_official_large
返回顶部