# n8n-nodes-docx-ooxml

This is an n8n community node that lets you create DOCX/OOXML documents using the powerful [docx.js](https://docx.js.org/) library in your n8n workflows.

Generate professional Word documents from your workflow data with high-level support for paragraphs, headings, tables, text formatting, and more.

[n8n](https://n8n.io/) is a [fair-code licensed](https://docs.n8n.io/sustainable-use-license/) workflow automation platform.

[Installation](#installation) | [Operations](#operations) | [Usage](#usage) | [Examples](#examples) | [Resources](#resources)

## Installation

Follow the [installation guide](https://docs.n8n.io/integrations/community-nodes/installation/) in the n8n community nodes documentation.

**Community Node Name:** `n8n-nodes-docx-ooxml`

## Operations

### Create Document

Create a new DOCX document from structured data with the following features:

- **Document metadata**: Set title and creator
- **Content sources**: Use input data or provide custom JSON
- **Output formats**: Binary DOCX file, Raw OOXML (XML), Base64, or Buffer
- **Rich formatting**: Headings, alignment, bold, italic, underline, colors
- **Tables**: Create structured tables with custom widths
- **Paragraphs**: Multiple text runs with different styles

## Credentials

No credentials required. This node works entirely locally within your n8n instance.

## Compatibility

- **Minimum n8n version**: 1.0.0
- **Tested with**: n8n 1.x
- **Dependencies**: docx.js v9.5.1+

## Usage

### Basic Usage

1. Add the **DOCX OOXML** node to your workflow
2. Select **Create Document** operation
3. Choose your content source (Input Data or Custom JSON)
4. Configure output format (Binary DOCX, OOXML, Base64, or Buffer)
5. Execute the workflow

### Content Structure

The node accepts structured JSON data to define document content:

```json
{
  "sections": [
    {
      "paragraphs": [
        {
          "text": "Hello World",
          "heading": "Heading1",
          "alignment": "center"
        },
        {
          "children": [
            {
              "text": "Bold text",
              "bold": true
            },
            {
              "text": " and ",
              "italics": true
            },
            {
              "text": "colored text",
              "color": "FF0000"
            }
          ]
        }
      ],
      "tables": [
        {
          "rows": [
            {
              "cells": ["Header 1", "Header 2", "Header 3"]
            },
            {
              "cells": ["Row 1 Col 1", "Row 1 Col 2", "Row 1 Col 3"]
            }
          ]
        }
      ]
    }
  ]
}
```

### Supported Properties

#### Paragraph Properties
- `text`: String or array of text runs
- `heading`: "Heading1" through "Heading6"
- `alignment`: "left", "center", "right", "justified"
- `pageBreak`: Boolean for page break before paragraph

#### Text Run Properties
- `text`: The text content
- `bold`: Boolean
- `italics`: Boolean
- `underline`: Boolean
- `color`: Hex color code (e.g., "FF0000")
- `size`: Font size
- `font`: Font family name

#### Table Properties
- `rows`: Array of row objects
- `cells`: Array of cell content (strings or objects)
- `width`: Cell width percentage

## Examples

### Example 1: Simple Document from Input Data

Input data:
```json
{
  "title": "Report",
  "content": "This is a simple report."
}
```

The node will automatically convert this to a document with key-value pairs.

### Example 2: Structured Document with Custom JSON

```json
{
  "sections": [
    {
      "paragraphs": [
        {
          "text": "Sales Report Q4 2024",
          "heading": "Heading1",
          "alignment": "center"
        },
        {
          "text": "Executive Summary",
          "heading": "Heading2"
        },
        {
          "children": [
            {
              "text": "Total Revenue: ",
              "bold": true
            },
            {
              "text": "$1,234,567",
              "color": "00FF00"
            }
          ]
        }
      ],
      "tables": [
        {
          "rows": [
            {
              "cells": ["Product", "Units Sold", "Revenue"]
            },
            {
              "cells": ["Product A", "1,000", "$50,000"]
            },
            {
              "cells": ["Product B", "2,500", "$125,000"]
            }
          ]
        }
      ]
    }
  ]
}
```

### Example 3: Output as Binary File

Set **Output Format** to "Binary (DOCX)" and **Binary Property Name** to "data". The document will be available as a downloadable file in subsequent nodes.

### Example 4: Output as Raw OOXML (XML)

Set **Output Format** to "OOXML (Raw XML)" to extract the raw XML content from the DOCX package. The output will include:

```json
{
  "ooxml": {
    "word/document.xml": "<w:document xmlns:w=\"...\">...</w:document>",
    "word/styles.xml": "...",
    "[Content_Types].xml": "...",
    "_rels/.rels": "..."
  },
  "mainDocument": "<w:document xmlns:w=\"...\">...</w:document>",
  "files": ["word/document.xml", "word/styles.xml", ...]
}
```

**Key OOXML files extracted:**
- `word/document.xml` - Main document content (also available in `mainDocument` field)
- `word/styles.xml` - Document styles
- `word/numbering.xml` - Numbering definitions
- `word/settings.xml` - Document settings
- `word/_rels/document.xml.rels` - Relationships
- `[Content_Types].xml` - Content types
- `_rels/.rels` - Package relationships

This is useful for:
- **Office.js Integration**: Use with `insertOoxml()` to insert content into Word documents
- Inspecting the raw OOXML structure
- Processing XML content with other tools
- Understanding document structure
- Debugging document generation

**Office.js Example:**
```javascript
// Insert OOXML into Word using Office.js
await Word.run(async (context) => {
    const range = context.document.getSelection();
    range.insertOoxml(data.mainDocument, Word.InsertLocation.replace);
    await context.sync();
});
```

See `examples/officejs-integration.md` for complete integration guide.

## Resources

* [n8n community nodes documentation](https://docs.n8n.io/integrations/#community-nodes)
* [docx.js documentation](https://docx.js.org/)
* [OOXML specification](http://officeopenxml.com/)

## Version History

### 0.1.0 (Initial Release)
- Create DOCX documents from structured data
- Support for paragraphs, headings, and tables
- Rich text formatting (bold, italic, underline, colors)
- Multiple output formats (Binary DOCX, Raw OOXML/XML, Base64, Buffer)
- Extract raw OOXML XML content from generated documents
- Based on docx.js v9.5.1 and jszip
