DOCX
Quick Start
Create your first .docx document in 5 minutes with @office-open/docx
Create a Word document from scratch in three steps: define content, generate a document, and export.
Step 1 — Install
pnpm add @office-open/docx
npm install @office-open/docx
yarn add @office-open/docx
bun add @office-open/docx
Step 2 — Create a Document
import { generateDocument } from "@office-open/docx";
const buffer = await generateDocument({
sections: [
{
children: [
// A title paragraph
{
paragraph: {
heading: "Heading1",
children: [{ text: "My First Document", bold: true, size: 32 }],
},
},
// A body paragraph
{
paragraph: {
children: [
"This is a paragraph with ",
{ text: "bold", bold: true },
" and ",
{ text: "italic", italic: true },
" text.",
],
},
},
],
},
],
});
Or use the JSON API:
{
"sections": [
{
"children": [
{
"paragraph": {
"heading": "Heading1",
"children": [{ "text": "My First Document", "bold": true, "size": 16 }]
}
},
{
"paragraph": {
"children": [
"This is a paragraph with ",
{ "text": "bold", "bold": true },
" and ",
{ "text": "italic", "italic": true },
" text."
]
}
}
]
}
]
}
import { generateDocument } from "@office-open/docx";
const buffer = await generateDocument({
sections: [
{
children: [
{
paragraph: {
heading: "Heading1",
children: [{ text: "My First Document", bold: true, size: 32 }],
},
},
{
paragraph: {
children: [
"This is a paragraph with ",
{ text: "bold", bold: true },
" and ",
{ text: "italic", italic: true },
" text.",
],
},
},
],
},
],
});
Step 3 — Export
// Node.js — write to file
import { writeFileSync } from "node:fs";
import { generateDocument } from "@office-open/docx";
const buffer = await generateDocument({
/* ...options... */
});
writeFileSync("output.docx", buffer);
// Browser — download as file
import { generateDocument } from "@office-open/docx";
const blob = await generateDocument(
{
/* ...options... */
},
{ type: "blob" },
);
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "output.docx";
a.click();
Output Formats
generateDocument() supports multiple output formats via the second argument. See the page for the full list.
| Usage | Returns | Use Case |
|---|---|---|
generateDocument(opts) | Buffer | Node.js file I/O |
generateDocument(opts, { type: "blob" }) | Blob | Browser downloads |
generateDocument(opts, { type: "base64" }) | string | Data URLs, API payloads |
generateDocument(opts, { type: "string" }) | string | Debugging, inspection |
generateDocumentStream(opts) | ReadableStream<Uint8Array> | Streaming large files |