$npx -y skills add sabahattink/antigravity-fullstack-hq --skill docx-officialGenerating Word documents programmatically with docx library in Node.js. Use when generating .docx reports, contracts, invoices, or any structured Word document from data.
| 1 | # Generating Word Documents with docx |
| 2 | |
| 3 | ## Installation |
| 4 | |
| 5 | ```bash |
| 6 | npm install docx |
| 7 | # or |
| 8 | pnpm add docx |
| 9 | ``` |
| 10 | |
| 11 | ## Basic Document |
| 12 | |
| 13 | ```typescript |
| 14 | import { |
| 15 | Document, Packer, Paragraph, TextRun, |
| 16 | HeadingLevel, AlignmentType, BorderStyle, |
| 17 | } from 'docx' |
| 18 | |
| 19 | async function generateReport(data: ReportData): Promise<Buffer> { |
| 20 | const doc = new Document({ |
| 21 | creator: 'Antigravity HQ', |
| 22 | title: data.title, |
| 23 | description: data.description, |
| 24 | styles: { |
| 25 | default: { |
| 26 | document: { |
| 27 | run: { |
| 28 | font: 'Calibri', |
| 29 | size: 24, // half-points (24 = 12pt) |
| 30 | color: '333333', |
| 31 | }, |
| 32 | }, |
| 33 | }, |
| 34 | }, |
| 35 | sections: [ |
| 36 | { |
| 37 | properties: {}, |
| 38 | children: [ |
| 39 | new Paragraph({ |
| 40 | text: data.title, |
| 41 | heading: HeadingLevel.HEADING_1, |
| 42 | spacing: { after: 200 }, |
| 43 | }), |
| 44 | new Paragraph({ |
| 45 | children: [ |
| 46 | new TextRun({ text: 'Generated: ', bold: true }), |
| 47 | new TextRun({ text: new Date().toLocaleDateString() }), |
| 48 | ], |
| 49 | spacing: { after: 400 }, |
| 50 | }), |
| 51 | ...data.paragraphs.map(text => |
| 52 | new Paragraph({ |
| 53 | text, |
| 54 | spacing: { after: 160 }, |
| 55 | alignment: AlignmentType.JUSTIFIED, |
| 56 | }) |
| 57 | ), |
| 58 | ], |
| 59 | }, |
| 60 | ], |
| 61 | }) |
| 62 | |
| 63 | return Packer.toBuffer(doc) |
| 64 | } |
| 65 | ``` |
| 66 | |
| 67 | ## Tables |
| 68 | |
| 69 | ```typescript |
| 70 | import { Table, TableRow, TableCell, WidthType, ShadingType } from 'docx' |
| 71 | |
| 72 | function createDataTable(headers: string[], rows: string[][]): Table { |
| 73 | return new Table({ |
| 74 | width: { size: 100, type: WidthType.PERCENTAGE }, |
| 75 | rows: [ |
| 76 | // Header row |
| 77 | new TableRow({ |
| 78 | tableHeader: true, |
| 79 | children: headers.map(header => |
| 80 | new TableCell({ |
| 81 | shading: { type: ShadingType.SOLID, color: '2563EB' }, |
| 82 | children: [ |
| 83 | new Paragraph({ |
| 84 | children: [new TextRun({ text: header, bold: true, color: 'FFFFFF' })], |
| 85 | }), |
| 86 | ], |
| 87 | }) |
| 88 | ), |
| 89 | }), |
| 90 | // Data rows |
| 91 | ...rows.map((row, rowIndex) => |
| 92 | new TableRow({ |
| 93 | children: row.map(cell => |
| 94 | new TableCell({ |
| 95 | shading: rowIndex % 2 === 0 |
| 96 | ? { type: ShadingType.SOLID, color: 'F8FAFC' } |
| 97 | : undefined, |
| 98 | children: [ |
| 99 | new Paragraph({ text: cell }), |
| 100 | ], |
| 101 | }) |
| 102 | ), |
| 103 | }) |
| 104 | ), |
| 105 | ], |
| 106 | }) |
| 107 | } |
| 108 | ``` |
| 109 | |
| 110 | ## Headers & Footers |
| 111 | |
| 112 | ```typescript |
| 113 | import { Header, Footer, PageNumber, NumberFormat } from 'docx' |
| 114 | |
| 115 | const doc = new Document({ |
| 116 | sections: [ |
| 117 | { |
| 118 | properties: { |
| 119 | page: { |
| 120 | size: { width: 12240, height: 15840 }, // Letter size in twips |
| 121 | margin: { top: 1440, right: 1440, bottom: 1440, left: 1440 }, |
| 122 | }, |
| 123 | pageNumberStart: 1, |
| 124 | pageNumberFormatType: NumberFormat.DECIMAL, |
| 125 | }, |
| 126 | headers: { |
| 127 | default: new Header({ |
| 128 | children: [ |
| 129 | new Paragraph({ |
| 130 | children: [ |
| 131 | new TextRun({ text: 'Antigravity HQ — Confidential', color: '6B7280', size: 18 }), |
| 132 | ], |
| 133 | border: { |
| 134 | bottom: { style: BorderStyle.SINGLE, size: 1, color: 'E5E7EB' }, |
| 135 | }, |
| 136 | }), |
| 137 | ], |
| 138 | }), |
| 139 | }, |
| 140 | footers: { |
| 141 | default: new Footer({ |
| 142 | children: [ |
| 143 | new Paragraph({ |
| 144 | children: [ |
| 145 | new TextRun({ text: 'Page ' }), |
| 146 | new TextRun({ children: [PageNumber.CURRENT] }), |
| 147 | new TextRun({ text: ' of ' }), |
| 148 | new TextRun({ children: [PageNumber.TOTAL_PAGES] }), |
| 149 | ], |
| 150 | alignment: AlignmentType.CENTER, |
| 151 | }), |
| 152 | ], |
| 153 | }), |
| 154 | }, |
| 155 | children: [/* document content */], |
| 156 | }, |
| 157 | ], |
| 158 | }) |
| 159 | ``` |
| 160 | |
| 161 | ## Lists |
| 162 | |
| 163 | ```typescript |
| 164 | import { LevelFormat } from 'docx' |
| 165 | |
| 166 | const doc = new Document({ |
| 167 | numbering: { |
| 168 | config: [ |
| 169 | { |
| 170 | reference: 'bullet-list', |
| 171 | levels: [ |
| 172 | { |
| 173 | level: 0, |
| 174 | format: LevelFormat.BULLET, |
| 175 | text: '•', |
| 176 | alignment: AlignmentType.LEFT, |
| 177 | style: { paragraph: { indent: { left: 720, hanging: 360 } } }, |
| 178 | }, |
| 179 | ], |
| 180 | }, |
| 181 | { |
| 182 | reference: 'numbered-list', |
| 183 | levels: [ |
| 184 | { |
| 185 | level: 0, |
| 186 | format: LevelFormat.DECIMAL, |
| 187 | text: '%1.', |
| 188 | alignment: AlignmentType.LEFT, |
| 189 | style: { paragraph: { indent: { left: 720, hanging: 360 } } }, |
| 190 | }, |
| 191 | ], |
| 192 | }, |
| 193 | ], |
| 194 | }, |
| 195 | sections: [ |
| 196 | { |
| 197 | children: [ |
| 198 | ...[ |
| 199 | 'First item', |
| 200 | 'Second item', |
| 201 | 'Third item', |
| 202 | ].map(text |