# Docx Generator

**The service is integrated into the following modules:**

- [Question Answerer](https://docs.unique.ai/administrators/space-management/create-spaces/unique-custom-space/add-ai-assistant-and-modules/question-answerer)

# Functionality

The **Docx Generator Service** enables developers to generate .docx files from predefined **templates**. It is used as part of a **tool** or **module**.  
This service is built on top of the [**docxtpl** library](https://docxtpl.readthedocs.io/en/latest/), which provides template-based document generation capabilities.

# Configuration

## Default Configuration

```json
{
  "uploadSuffix": "_report_output.docx",
  "uploadScopeId": null,
  "uploadToChat": true,
  "templateName": "template.docx",
  "templateScopeId": null,
  "templateFields": {
    "date": "CURRENT_DATE",
    "document_title": "Template Document"
  },
  "skipIngestion": true
}
```

| **Field Name**        | **Description**                                                                    | **Type** | **Default Value**                | **Required** |
| ---------------------- | ---------------------------------------------------------------------------------- | -------- | ---------------------------------- | ------------ |
| `uploadSuffix`        | The suffix of the generated docx file.                                            | string   | `_report_output.docx`            | No           |
| `uploadScopeId`      | The scope id into which the generated docx file is uploaded.                     | string   | `null`                            | No           |
| `uploadToChat`       | Whether to upload the generated file to the chat.                                | boolean  | `true`                            | No           |
| `templateName`       | The name of the template file.                                                    | string   | `template.docx`                  | No           |
| `templateScopeId`    | The scope id of the template.                                                     | string   | `null`                            | No           |
| `templateFields`      | The fields to be replaced in the template.                                       | string   | `{"date": CURRENT_DATE, "document_title": "Template Document"}` | No           |
| `skipIngestion`       | Whether to skip the ingestion (chunking & embedding) of the generated file when uploaded to the knowledge base. | boolean  | `true`                            | No           |

## Nested Types

### TemplateFields

When customizing `templateFields`, ensure that each placeholder defined in your template has a corresponding field in the configuration.

To automatically insert the current date into your document, set the `date` field value to `CURRENT_DATE`.

| Field Name         | Description                                                                 | Default Value | Type   | Required |
| ------------------- | --------------------------------------------------------------------------- | ------------- | ------ | -------- |
| `date`             | The date to be inserted in the template. If not explicitly defined, the current date will be selected. | `CURRENT_DATE` | string | No       |
| `document_title`   | The title of the document.                                                  | `Template Document` | string | No       |

## Default Unique Template

Below you can find the default template that is used by the docxGenerator if no template is configured.

[DOCXDoc Template.docxWord documentDownload](https://unique-ch.atlassian.net/wiki/rest/api/content/1410433218/child/attachment/att1414234277/download)

The `List Bullet` style must be added to `Quick Style List` in the template to have the docxGenerator work properly.

The **colored boxes** indicate how each section of the document is handled:

- **Blue - Fixed Content:** These contain **static elements** that remain the same across all generated documents. This content is hardcoded into the template and is **not dynamic**. 
  - Examples include:
    - Client logo
    - Footer disclaimers
    - Page numbers
- **Orange - Configurable Content:** Text in orange boxes is **dynamically populated** from the configuration.  
For example, if your configuration contains:

```json
"templateFields": {
    "document_title": "Template Document"
}
```
The service will replace the placeholder `{{document_title}}` in the template with "Template Document" during generation.
  - **Important:** The inserted text will inherit the style of the placeholder it replaces (e.g., font, size, colour).
  - **Note:** The service automatically replaces the `CURRENT_DATE` placeholder with the current date at runtime.
- **Red Box – Programmable Content Areas:** This is a **required placeholder** used by module/tool to inject content into the document via code. The structure and presence of this box must remain unchanged in the template to allow proper integration.

# Styling the template

Content added to the template, whether through code or by replacing placeholders, will **inherit the styles defined in the template**. Make sure to customize these styles in the template file to match your formatting and branding requirements.

## Notes

- Ensure that all fields are correctly configured to match the intended document generation and upload requirements.
- The default values provided offer a baseline configuration that can be customized as needed.
- Consider the implications of skipping ingestion and uploading to chat based on operational needs and workflow integration.
