better docx

Get Started

Getting started with betterdocx

Installation

npm install betterdocx

Then you can import as usual:

import * as docx from "betterdocx";
// or
import { ... } from "betterdocx";

Basic Usage

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

// Documents contain sections, you can have multiple sections per document, go here to learn more about sections
// This simple example will only contain one section
const doc = new Document({
    sections: [
        {
            properties: {},
            children: [
                new Paragraph({
                    children: [
                        new TextRun("Hello World"),
                        new TextRun({
                            text: "Foo Bar",
                            bold: true,
                        }),
                        new TextRun({
                            text: "\tGithub is the best",
                            bold: true,
                        }),
                    ],
                }),
            ],
        },
    ],
});

// Used to export the file into a .docx file
Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("My Document.docx", buffer);
});

// Done! A file called 'My Document.docx' will be in your file system.

clippy the assistant

A Fork

This is a fork of the excellent docxjs library by dolanmiu

On this page