Install
openclaw skills install docx-tablesCreate 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.
openclaw skills install docx-tablesCreate Word documents with tables that work everywhere - Word, Google Docs, etc.
Set widths in TWO places - on table AND on each cell:
// Table level
new Table({
width: { size: 9360, type: WidthType.DXA },
columnWidths: [1872, 7488],
rows: [...]
})
// Cell level - EVERY cell needs width!
new TableCell({
width: { size: 1872, type: WidthType.DXA },
children: [...]
})
Percentages break in Google Docs. Use DXA:
// ❌ WRONG
width: { size: 100, type: WidthType.PERCENTAGE }
// ✅ CORRECT
width: { size: 9360, type: WidthType.DXA }
const { ShadingType } = require('docx');
// ❌ WRONG - black background!
shading: { type: ShadingType.SOLID, fill: "E0F2F1" }
// ✅ CORRECT
shading: { type: ShadingType.CLEAR, fill: "E0F2F1" }
const cellMargins = { top: 80, bottom: 80, left: 120, right: 120 };
new TableCell({
margins: cellMargins,
children: [...]
})
For 1" margins: 9360 DXA
columnWidths: [1872, 7488] // = 9360 ✓
columnWidths: [3120, 3120, 3120] // = 9360 ✓
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);
});
For US Letter with 1" margins = 9360 DXA:
| Layout | Column Widths |
|---|---|
| 2 cols (20/80) | [1872, 7488] |
| 2 cols (25/75) | [2340, 7020] |
| 2 cols (equal) | [4680, 4680] |
| 3 cols (equal) | [3120, 3120, 3120] |
| 3 cols (25/25/50) | [2340, 2340, 4680] |
Tables narrow in Google Docs?
WidthType.DXA, not PERCENTAGEwidth to EVERY TableCellCell backgrounds black?
ShadingType.CLEAR, not SOLIDText touching borders?
margins: { top: 80, bottom: 80, left: 120, right: 120 }Columns uneven?