mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
* feat: Word's themes, colors and fonts, and the theme of a Word document or template, from docx/theme (#878) docx/theme is a new optional entry, so the main bundle stays as it is: - Themes: the 29 themes of Word's Design > Themes gallery, each with its name, colors, and fonts for headings and body text, as IThemeOptions. - ColorSchemes and FontSchemes: the 24 color sets and 26 font sets of the Design > Colors and Design > Fonts galleries, to mix with a theme as Word's Design tab does. - readTheme: the theme of a .docx, .docm, .dotx, .dotm, .pptx, .xlsx or .thmx file, or of a theme1.xml, or colors or fonts Word saved on their own. Styles taken from a template with externalStyles then keep the template's theme fonts and colors (#878). The presets are generated from Word for Mac's own theme files by scripts/generate-word-themes.ts, which reads each one with readTheme. In docx, a theme font can now have a font for each script (scripts, by ISO 15924 code), in place of Office's for those scripts, so Word's themes keep their own fonts for Japanese, Chinese and other scripts. docx also exports parseXml and unzipFiles, which it already had, so docx/theme reads files without bundling a second XML parser and unzipper. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * refactor: read a template's theme with readTheme from docx, sharing the patcher's theme reader, without docx/theme (#878) - Word's themes, colour sets and font sets (Themes, ColorSchemes, FontSchemes), the script that generated them and the docx/theme entry are dropped. #878 asks only for the theme of the template a document's styles come from. - readTheme moves into the core and is exported from docx. The patcher reads the colours of a template's theme with the same code, so parseXml and unzipFiles stay internal. - The paths of a package's parts and its relationships' targets (relationshipsPathOf, resolveTarget and the others) move from the patcher to src/util/package-paths.ts, so the theme reader in src/file can use them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * fix: read a theme in UTF-16, and inflate only a package's relationships and its theme in readTheme - A theme, or the XML of one, that starts with UTF-16's byte order mark is read as UTF-16, little or big endian, as XML may be written. - readTheme inflates a package's relationships, and then only the theme they refer to, so a large or damaged document, presentation or workbook part doesn't take memory or stop it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c * fix: find a theme whose relationship's target is in another case than its part, in readTheme and the patcher The Open Packaging Conventions compare part names without regard to case, so a relationship's target such as "Theme/THEME1.xml" names word/theme/theme1.xml. readTheme threw that the file has no theme, and patchDocument used Office's colours. isSamePart, in src/util/package-paths.ts, compares them as drawing-patch already finds a relationship's target. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c --------- 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 💖
