Skip to main content

This is a Deno module for making .docx files.

It can be used to create a .docx using components, for example:

/** @jsx Docx.jsx */
import Docx, { Paragraph } from 'https://deno.land/x/docxml/mod.ts';

await Docx.fromJsx(<Paragraph>This is the simplest document you could make.</Paragraph>)
    .toFile('example-1.docx');

Or it can be used to convert any XML to DOCX:

/** @jsx Docx.jsx */
import Docx, { Paragraph, Text } from 'https://deno.land/x/docxml/mod.ts';

await Docx.fromNothing()
    .withXmlRule('self::node()', ({ traverse }) => traverse('./*'))
    .withXmlRule('self::text()', ({ node }) => <Text>{node.nodeValue}</Text>)
    .withXmlRule('self::p', ({ traverse }) => <Paragraph>{traverse()}</Paragraph>)
    .withXmlRule('self::strong', ({ traverse }) => <Text isBold>{traverse()}</Text>)
    .withXml(
        `<html>
            <body>
                <p>This is a very simply <strong>XML transformation</strong>.</p>
            </body>
        </html>`,
        {},
    )
    .toFile('example-2.docx');

For the latest and greatest API documentation please go to https://doc.deno.land/https://deno.land/x/docxml/mod.ts

Formatting options

Paragraph formatting

Paragraph styles may be applied via different ways;

// As a prop:
<Paragraph alignment="center" />
// As a style:
const style = docx.styles.add({
    type: 'paragraph',
    paragraph: {
        alignment: 'center',
    },
});
<Paragraph style={style} />;

Text formatting

// As a prop:
<Text isBold />
  • See all the text formatting options
  • Paragraph formatting options may be merged with text formatting options. When used directly on a component (ie. not via a style) the formatting only applies to the paragraph pilcrow (“¶”) sign. In MS Word, the text styling options merged into a paragraph style definition do apply to the paragraph text – but may still be overriden with dedicated text formatting options.
<Paragraph isBold>Text not shown as bold, but the paragraph's pilcrow is.</Paragraph>
// As a style:
const style = docx.styles.add({
    type: 'paragraph',
    text: {
        isItalic: true,
    },
    paragraph: {
        isBold: true,
    },
});
<Paragraph style={style}>Text is shown as bold and italic</Paragraph>;

Features

To great or small extend, the following features work in the current version of docxml. Some items are not ticked off yet – they are not available, but hopefully soon:

Custom styles:

  • Font size and color
  • Bold, italic, underline styles
  • Subscript, superscript, small caps
  • Paragraph spacing and indentation
  • Left/right/center/justified alignment
  • Aligning text on tabs
  • Font family

Tables:

Images:

  • From any UInt8Array source
  • Alternative and title text
  • Width and height

Sections:

  • Width and height
  • Orientation

Comments:

  • Point comment
  • Range comment
  • Comment reply

Change tracking:

  • Text additions and deletions
  • Style changes
  • Table row additions and deletions

Differences with actual MS Word DOCX

Obviously docxml is a TypeScript project, which is already very different from how you would normally interact with a DOCX document. More meaningfully however, docxml is meant to make writing DOCX easier. For example;

  • All sizes are of type Length, which means it doesn’t matter wether you input them as points, centimeters, inches, 1/2, 1/8th or 1/20th points, English Metric Units, and so on.
  • The JSX pragma will try to correct components that would lead to invalid XML structures, by splitting the parents of invalidly placed components recursively until the new position is valid. Moreover, string content in unexpected places is automatically wrapped in <Text> when using JSX.
  • Using the <Image> or <Comment> components will automatically create all required relationships etc.
  • Some of the words have changed, generally speaking docxml is more verbose than the DOCX verbiage.
  • Generally speaking docxml prefers formal (JS) references over references-by-identifier. The identifiers are generated for you when the .docx file is written.
  • Especially in tables and images, a lot of formatting details are automatically applied. In a lot of cases there is no API yet to change them.

For contributors

# Run all unit tests
deno task test


# Run all linting
deno task lint