Files
docx/docs/quickstart.md
T
DolanandClaude Opus 5.5 c169dd1a83 feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded (#3556)
* 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>
2026-09-28 13:00:29 +01:00

5.4 KiB

Quickstart Guide

Get up and running with docx in under 5 minutes.

In this guide:

  • Install docx
  • Create your first document
  • Add text, tables, and images
  • Export in multiple formats

Installation

npm install --save docx

Your First Document

Create a "Hello World" document in just a few lines:

import { Document, Packer, Paragraph, TextRun } from "docx";
import * as fs from "fs";

const doc = new Document({
    sections: [
        {
            // Documents are organized into sections
            children: [
                new Paragraph({
                    children: [new TextRun("Hello World!")],
                }),
            ],
        },
    ],
});

Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("My Document.docx", buffer);
});

That's it! Run this code to create your first Word document.

Adding Common Elements

Bold and Styled Text

import { Document, Paragraph, TextRun } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new TextRun("Normal text, "),
                        new TextRun({
                            text: "bold text, ",
                            bold: true,
                        }),
                        new TextRun({
                            text: "italic text",
                            italics: true,
                        }),
                    ],
                }),
            ],
        },
    ],
});

See the Text guide for all formatting options.

Headings

import { Document, HeadingLevel, Paragraph } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    text: "My Heading",
                    heading: HeadingLevel.HEADING_1,
                }),
                new Paragraph("Some text under the heading."),
            ],
        },
    ],
});

Bullet Points

import { Document, Paragraph } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    text: "First item",
                    bullet: { level: 0 },
                }),
                new Paragraph({
                    text: "Second item",
                    bullet: { level: 0 },
                }),
                new Paragraph({
                    text: "Nested item",
                    bullet: { level: 1 },
                }),
            ],
        },
    ],
});

See the Bullet Points guide for more options.

Tables

import { Document, Paragraph, Table, TableCell, TableRow } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                new Table({
                    rows: [
                        new TableRow({
                            children: [
                                new TableCell({ children: [new Paragraph("Cell 1")] }),
                                new TableCell({ children: [new Paragraph("Cell 2")] }),
                            ],
                        }),
                    ],
                }),
            ],
        },
    ],
});

See the Tables guide for advanced table features.

Images

import { Document, ImageRun, Paragraph } from "docx";
import * as fs from "fs";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ImageRun({
                            type: "png",
                            data: fs.readFileSync("./demo/assets/images/dog.png"),
                            transformation: {
                                width: 200,
                                height: 200,
                            },
                        }),
                    ],
                }),
            ],
        },
    ],
});

See the Images guide for positioning and wrapping options.

Export Options

Node.js - Save to File

import * as fs from "fs";

Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("document.docx", buffer);
});

Node.js - Stream

import * as fs from "fs";

Packer.toStream(doc).then((stream) => {
    stream.pipe(fs.createWriteStream("document.docx"));
});

Browser - Download

import { saveAs } from "file-saver";

Packer.toBlob(doc).then((blob) => {
    saveAs(blob, "document.docx");
});

Base64 String

Packer.toBase64String(doc).then((base64) => {
    console.log(base64);
});

See the Packers guide for all export options.

Next Steps

Now that you've created your first document, explore these guides:

?> Check out the demo folder for 90+ working examples covering every feature.