mirror of
https://github.com/dolanmiu/docx.git
synced 2026-12-01 16:55:59 +01:00
* feat(table): write each cell's width from columnWidths, so Word keeps them Word lays a table out from its cells' widths (w:tcW), and sizes the columns of cells without one to their text, whatever the grid (w:tblGrid) says. docx wrote columnWidths only to the grid, so in Word they did nothing unless every cell was given the same width again, as #517, #352, #1099 and #561 found. A table saved as a PDF from Word 16 for Mac showed it: "Entry" shrank to its text and gave its room to the other columns. Each cell without a width of its own now gets the width of the columns it spans, the cells added for rowSpan included. When the table's width is wider than its columns add up to, the cells' widths are scaled up to fill it, as Word and LibreOffice lay the columns out, so columnWidths given as proportions of a 100% table, as some issues suggest, keep them. The grid is written as given. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken * feat(styles): write Normal Table, the default table style, with Word's cell margins docx wrote no table style, and without one Word gives cells no margins, so their text touched the borders: in a PDF saved from Word, the text of each cell started half a point inside its border. LibreOffice gives them the 0.075 inches (108 twips) on the left and right that Word gives the tables it makes, so the same table had narrower lines of text there. docx now writes Normal Table as Word writes it in its own documents, the default table style with those margins, so tables look the same in Word as tables made in it, and as in LibreOffice. A Normal Table in externalStyles takes its place, and margins given to a table or cell still win. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken * fix(layout): lay cells out at their own widths, with the margins of their table's style The layout took each cell's width from the grid and gave every cell margins of 5.4 points, as LibreOffice does. Word goes by a cell's own width in twips when it has one, and takes cell margins from the table's style, or the default table style, and gives none without either. Cells are now laid out at their own width in twips when they have one, and their margins come from the table's style and the styles it is based on, or the default table style when it has none, then the table's own. With docx's Normal Table, that is the 5.4 points it was, and the table demos' headings stay on LibreOffice's pages. The docs say to give tables columnWidths for their page numbers to be right, as Word sizes the columns of a table without widths to their text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken * docs(table): don't suggest columnWidths as proportions for readers that go by the grid The warning about readers that lay tables out from the grid alone ended by suggesting columnWidths as proportions of a table with a width. Only the cells' widths are scaled to the table's width, and the grid is written as given, so those readers still take [20, 80] as 20 and 80 twips. The warning asks for real twips again, and the paragraph on scaling says it applies to the readers that go by the cells' widths. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken * fix(layout): stop at a table whose rows give a column different widths With cells laid out at their own widths, two rows could give the same column different widths, such as a one-column table whose first cell is 1000 twips wide and whose second is 3000. Word settles the column's width in a way the layout doesn't follow yet, so at least one row's lines were broken at the wrong width. The layout now stops at such a table and leaves the page numbers after it blank, as it does for other things it can't lay out. Widths a point apart, from rounding, still count as equal. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Welcome to docx
What is docx?
docx is a TypeScript/JavaScript library for generating Word documents (.docx files) programmatically. It provides a declarative API that works seamlessly in both Node.js and browser environments.
Why docx?
- Declarative API - Define what you want (objects/config), not how to build it step-by-step
- TypeScript First - Full type definitions with IntelliSense support
- Universal - Works in Node.js, browsers, and serverless environments
- No Dependencies - Zero external runtime dependencies
- Full Featured - Tables, images, shapes, charts, headers, footers, styles, and more
Quick Navigation
| I want to... | Go to... |
|---|---|
| Get started quickly | Quickstart Guide |
| Learn a specific feature | Usage Guides |
| See working examples | Demo Files |
| Browse the API | API Documentation |
| Modify existing documents | Patcher |
Installation
npm install --save docx
Then you can require or import as usual:
const docx = require("docx");
import * as docx from "docx";
// or
import { Document, Packer, Paragraph, TextRun } from "docx";
Shapes, watermarks and charts come with the package too, from docx/shapes, docx/watermarks and docx/charts:
import { ShapeRun } from "docx/shapes";
import { TextWatermark } from "docx/watermarks";
import { ChartRun } from "docx/charts";
Basic Usage
import * as fs from "fs";
import { Document, Packer, Paragraph, TextRun } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new TextRun("Hello World"),
new TextRun({
text: " - Bold text",
bold: true,
}),
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});
Made with 💖
