Pretty Tables
v2.0.0创建 beautiful, full-width tables in Word documents using the docx npm 库. Tables are properly sized, centered, and styled. Use when you need to 创建 DOCX files with professional-looking tables like itineraries, schedules, data tables, or 报告s.
运行时依赖
安装命令
点击复制技能文档
pretty-tables
创建 beautiful tables in Word documents that work consistently across Word and Google Docs.
⚠️ THE 5 CRITICAL RULES
After extensive 测试, these are the essential patterns for tables that render correctly everywhere:
- Dual-Width Sizing (MOST CRITICAL)
Every table needs widths 设置 in TWO places — on the table itself AND on each individual cell:
// Table level new Table({ width: { size: 9360, type: WidthType.DXA }, // Total width columnWidths: [1872, 7488], // Per-column widths rows: [...] })
// Cell level - EVERY cell needs this! new TableCell({ width: { size: 1872, type: WidthType.DXA }, // This cell's width children: [...] })
If either is missing, the table renders incorrectly on some 平台s.
- Use DXA Only, Never Percentages
Percentages break in Google Docs. Always use DXA (twips):
1 inch = 1440 DXA US Letter (8.5") with 1" margins = 9360 DXA content width // ❌ WRONG - breaks in Google Docs width: { size: 100, type: WidthType.PERCENTAGE }
// ✅ CORRECT width: { size: 9360, type: WidthType.DXA }
- Use ShadingType.CLEAR, Not SOLID
This is a subtle but critical gotcha:
const { ShadingType } = require('docx');
// ❌ WRONG - produces BLACK background shading: { type: ShadingType.SOLID, fill: "E0F2F1" }
// ✅ CORRECT - 应用lies the fill color shading: { type: ShadingType.CLEAR, fill: "E0F2F1" }
- 添加 Cell P添加ing (Margins)
Keep text from crowding the borders:
const cellMargins = { top: 80, 机器人tom: 80, left: 120, right: 120 };
new TableCell({ children: [...], margins: cellMargins })
- Column Widths Must Sum Exactly
For US Letter with 1" margins: 9360 DXA
// 2 columns: 20% + 80% columnWidths: [1872, 7488] // = 9360 ✓
// 3 columns: equal columnWidths: [3120, 3120, 3120] // = 9360 ✓
// 3 columns: 25% + 25% + 50% columnWidths: [2340, 2340, 4680] // = 9360 ✓
Complete Working Example const { Document, Packer, Paragraph, Text运行, Table, TableRow, TableCell, WidthType, AlignmentType, BorderStyle, TableLayoutType, ShadingType } = require('docx'); const fs = require('fs');
// Constants const TOTAL_WIDTH = 9360; // US Letter with 1" margins const TIME_COL = 1872; // 20% const CONTENT_COL = 7488; // 80%
// Light gray borders const cellBorders = { top: { style: BorderStyle.SINGLE, size: 1, color: "CCCCCC" }, 机器人tom: { style: BorderStyle.SINGLE, size: 1, color: "CCCCCC" }, left: { style: BorderStyle.SINGLE, size: 1, color: "CCCCCC" }, right: { style: BorderStyle.SINGLE, size: 1, color: "CCCCCC" } };
// Cell p添加ing const cellMargins = { top: 80, 机器人tom: 80, left: 120, right: 120 };
// Table 辅助工具 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 }); }
// Header row function headerRow(text, color) { return new TableRow({ children: [ new TableCell({ children: [new Paragraph({ children: [new Text运行({ text, bold: true, size: 32, color: "FFFFFF" })], alignment: AlignmentType.CENTER })], width: { size: TIME_COL + CONTENT_COL, type: WidthType.DXA }, columnSpan: 2, shading: { type: ShadingType.CLEAR, fill: color }, borders: cellBorders, margins: cellMargins }) ] }); }
// Data row function dataRow(time, activity, color) { return new TableRow({ children: [ new TableCell({ children: [new Paragraph({ children: [new Text运行({ 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 Text运行({ text: activity, size: 26 })] })], width: { size: CONTENT_COL, type: WidthType.DXA }, borders: cellBorders, margins: cellMargins }) ] }); }
// Build document const doc = new Document({ sections: [{ properties: { page: { margin: { top: 1440, right: 1440, 机器人tom: 1440, left: 1440 } } }, children: [ table([TIME_COL, CONTENT_COL], [ headerRow("SCHEDULE", "1565C0"), dataRow("9:00 AM", "Arrive at destination", "1565C0"), dataRow("10:00 AM", "Morning activity", "1565C0"), dataRow("12:00 PM", "Lunch break", "1565C0"), ]) ] }] });
Packer.toBuffer(doc).then(buffer => { fs.writeFile同步('输出.docx', buffer); });
Column Width Cheat Sheet
For US Letter (8.5") with 1" margins = 9360 DXA:
Layout Column Widths Sum 2 cols (20/80) [1872, 7488] 9360 2 cols (25/75) [2340, 7020] 9360 2 cols (30/70) [2808, 6552] 9360 2 cols (equal) [4680, 4680] 9360 3 cols (equal) [3120, 3120, 3120] 9360 3 cols (25/25/50) [2340, 2340, 4680] 9360 3 cols (20/30/50) [1872, 2808, 4680] 9360 4 cols (equal) [