mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
* feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded The docs' code examples were plain snippets, often fragments that couldn't run. A code block marked ```ts live is now an example readers can see and change: the page runs it with docx, draws the document it makes next to the code with docx-preview, and offers the .docx to download. Clicking the code opens an editor (Monaco) that knows docx's types, the pages are drawn again as the code changes, and View closes the editor. Split, Code and Preview show the code and the pages side by side, or one of them. The demos a page includes are live too. Every example in the docs that builds or patches a document is now live: 443 of them, each a whole file with its imports, so it runs as written. Code about other things, such as installing, bundlers or servers, stays plain. Running them showed docs that were wrong, fixed here: - Landscape: docx swaps a landscape page's width and height itself, so the docs' swapped sizes made a tall page marked as landscape - Comments: `comments.children` takes plain objects, not `new Comment(...)` - Custom properties are text only - APIs that no longer exist: MathText, Row, `.then` on Packer.toStream, patchDocument(buffer, ...), createParagraph().heading1(), a border's `value`, TextRun's `.break()` - The templates and patcher examples read files that aren't in the repository. They now patch the templates in demo/assets, or make their own `npm run build.docs` copies docx's browser builds into docs/live-examples/lib, so the docs use the docx they describe, and the Pages workflow runs it. The folder is gitignored, and cspell and ESLint skip it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ * docs: the bullet examples draw a bullet, and the patcher's usage writes the patched document "ὠ" is U+1F60, a Greek omega, since \u takes four hex digits, so the two live bullet examples on the numbering page drew it as each item's bullet. They use "•" now. The patcher's usage example now awaits patchDocument and writes the document it makes, as the example after it does. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ --------- Co-authored-by: Claude Opus 5.5 (1M context) <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 💖
