Skip to content
Skillv1.0.0

docx

Generate Word documents programmatically with the docx library — create paragraphs, tables, images, headers, footers, numbered lists, and styled content. Use when tasks involve generating contracts, p

by terminalskills(0) 0 installs
Free
Sign in to install

Free account. Installing gives you the manifest plus copy-paste snippets.

See reviews

About

Imported from terminalskills/skills (skills/docx/SKILL.md). Install upstream with npx skills add terminalskills/skills --skill docx. Copyright stays with the author (Apache-2.0).

docx

Generate .docx files from code. No Word installation needed.

Setup

# Install the docx library for Word document generation.
npm install docx

Basic Document

// src/docx/basic.ts — Create a Word document with a heading, paragraph, and save.
import { Document, Packer, Paragraph, TextRun, HeadingLevel } from "docx";
import fs from "fs";

const doc = new Document({
  sections: [
    {
      children: [
        new Paragraph({
          heading: HeadingLevel.HEADING_1,
          children: [new TextRun({ text: "Monthly Report", bold: true })],
        }),
        new Paragraph({
          spacing: { before: 200 },
          children: [
            new TextRun("This report covers performance metrics for "),
            new TextRun({ text: "January 2025", bold: true }),
            new TextRun(". All figures are preliminary."),
          ],
        }),
      ],
    },
  ],
});

const buffer = await Packer.toBuffer(doc);
fs.writeFileSync("report.docx", buffer);

Tables

// src/docx/tables.ts — Create a formatted table with header row and data.
import {
  Document, Packer, Paragraph, Table, TableRow, TableCell,
  TextRun, WidthType, BorderStyle, ShadingType,
} from "docx";
import fs from "fs";

function createHeaderCell(text: string): TableCell {
  return new TableCell({
    children: [new Paragraph({ children: [new TextRun({ text, bold: true, color: "FFFFFF" })] })],
    shading: { type: ShadingType.SOLID, color: "3498DB" },
  });
}

function createCell(text: string): TableCell {
  return new TableCell({
    children: [new Paragraph(text)],
  });
}

const table = new Table({
  width: { size: 100, type: WidthType.PERCENTAGE },
  rows: [
    new TableRow({ children: [createHeaderCell("Name"), createHeaderCell("Revenue"), createHeaderCell("Growth")] }),
    new TableRow({ children: [createCell("Product A"), createCell("$45,000"), createCell("+12%")] }),
    new TableRow({ children: [createCell("Product B"), createCell("$32,000"), createCell("+8%")] }),
  ],
});

const doc = new Document({ sections: [{ children: [table] }] });
const buffer = await Packer.toBuffer(doc);
fs.writeFileSync("table.docx", buffer);

Images

// src/docx/images.ts — Embed images in a Word document from file or URL.
import { Document, Packer, Paragraph, ImageRun } from "docx";
import fs from "fs";

const imageBuffer = fs.readFileSync("logo.png");

const doc = new Document({
  sections: [
    {
      children: [
        new Paragraph({
          children: [
            new ImageRun({
              data: imageBuffer,
              transformation: { width: 200, height: 60 },
              type: "png",
            }),
          ],
        }),
        new Paragraph("Company logo above."),
      ],
    },
  ],
});

const buffer = await Packer.toBuffer(doc);
fs.writeFileSync("with-image.docx", buffer);

Headers, Footers, and Page Numbers

// src/docx/headers.ts — Add headers and footers with page numbers.
import {
  Document, Packer, Paragraph, TextRun, Header, Footer,
  PageNumber, AlignmentType,
} from "docx";
import fs from "fs";

const doc = new Document({
  sections: [
    {
      headers: {
        default: new Header({
          children: [
            new Paragraph({
              alignment: AlignmentType.RIGHT,
              children: [new TextRun({ text: "Confidential", italics: true, color: "999999" })],
            }),
          ],
        }),
      },
      footers: {
        default: new Footer({
          children: [
            new Paragraph({
              alignment: AlignmentType.CENTER,
              children: [
                new TextRun("Page "),
                new TextRun({ children: [PageNumber.CURRENT] }),
                new TextRun(" of "),
                new TextRun({ children: [PageNumber.TOTAL_PAGES] }),
              ],
            }),
          ],
        }),
      },
      children: [
        new Paragraph({ text: "Document content goes here." }),
      ],
    },
  ],
});

const buffer = await Packer.toBuffer(doc);
fs.writeFileSync("with-headers.docx", buffer);

Lists

// src/docx/lists.ts — Create bulleted and numbered lists.
import { Document, Packer, Paragraph, TextRun, AlignmentType } from "docx";
import fs from "fs";

const doc = new Document({
  numbering: {
    config: [
      {
        reference: "numbered-list",
        levels: [
          { level: 0, format: "decimal", text: "%1.", alignment: AlignmentType.LEFT },
        ],
      },
    ],
  },
  sections: [
    {
      children: [
        new Paragraph({ text: "Action Items:", heading: "Heading2" as any }),
        ...[
          "Review Q1 metrics",
          "Update pricing model",
          "Schedule team sync",
        ].map(
          (item) =>
            new Paragraph({
              children: [new TextRun(item)],
              numbering: { reference: "numbered-list", level: 0 },
            })
        ),
      ],
    },
  ],
});

const buffer = await Packer.toBuffer(doc);
fs.writeFileSync("lists.docx", buffer);

Use it

Copy one of these into your project. Installing also returns the manifest and these snippets.

yaml
targets:
  - https://api.opensmartroute.ai/api/v1/registry/terminalskills-skills-docx/manifest   # or paste the manifest below

Manifest

An Open Capability Manifest: the router reads it to know what this does, what it costs and when to pick it.

terminalskills-skills-docx.ocm.jsonjson
{
  "ocm": "1",
  "id": "terminalskills-skills-docx",
  "kind": "skill",
  "name": "docx",
  "description": "Generate Word documents programmatically with the docx library — create paragraphs, tables, images, headers, footers, numbered lists, and styled content. Use when tasks involve generating contracts, proposals, resumes, or any .docx output from application data in Node.js.",
  "publisher": "terminalskills",
  "version": "1.0.0",
  "capabilities": {
    "domains": [
      "general"
    ],
    "tags": [
      "skill-md",
      "docx",
      "word",
      "document",
      "reports",
      "office",
      "skills-sh"
    ],
    "languages": [
      "en"
    ]
  },
  "quality_prior": 0.6,
  "examples": [
    "Generate Word documents programmatically with the docx library — create paragraphs, tables, images, headers, footers, numbered lists, and styled content. Use when tasks involve generating contracts, proposals, resumes, or any .docx output from application data in Node.js."
  ],
  "primary": false,
  "metadata": {
    "source": {
      "provider": "skills.sh",
      "repository": "https://github.com/terminalskills/skills",
      "path": "skills/docx/SKILL.md",
      "ref": "HEAD",
      "url": "https://github.com/terminalskills/skills/blob/HEAD/skills/docx/SKILL.md",
      "key": "terminalskills/skills/skills/docx/SKILL.md"
    },
    "compatibility": "Node.js 14+ or browser",
    "license": "Apache-2.0"
  },
  "instructions": "# docx\n\nGenerate .docx files from code. No Word installation needed.\n\n## Setup\n\n```bash\n# Install the docx library for Word document generation.\nnpm install docx\n```\n\n## Basic Document\n\n```typescript\n// src/docx/basic.ts — Create a Word document with a heading, paragraph, and save.\nimport { Document, Packer, Paragraph, TextRun, HeadingLevel } from \"docx\";\nimport fs from \"fs\";\n\nconst doc = new Document({\n  sections: [\n    {\n      children: [\n        new Paragraph({\n          heading: HeadingLevel.HEADING_1,\n          children: [new TextRun({ text: \"Monthly Report\", bold: true })],\n        }),\n ",
  "cost": {
    "context_tokens": 1273
  }
}

Fetch it by URL: GET /api/v1/registry/terminalskills-skills-docx/manifest?version=1.0.0

Reviews

Star ratings from people who tried it. One review per account; edit yours any time.

No reviews yet. Install it, try it, and be the first to rate it.