# XLSX Text Extraction

> Extracts tabular data from Microsoft Excel (.xlsx) workbooks using openpyxl.

Source: https://docs.sommatic.ai/en/nodes/ai.xlsx.extract.text/

- Slug: `ai.xlsx.extract.text`
- Family: AI
- Version: 1.0.0
- Status: Available
- Execution: async

Extracts tabular data from Microsoft Excel (.xlsx) workbooks using openpyxl.

Iterates every non-empty sheet in workbook order and renders each as a standalone markdown table, producing a single normalized text output suitable for downstream LLM consumption, vector indexing, or direct classification.

**Features:**
- **Per-sheet rendering:** Each sheet is emitted with its name as a header followed by a markdown table of its used range.
- **Formatted cell values:** Applies the workbook's display formats for numbers, dates, and percentages so the extracted text matches what a user sees in Excel.
- **Merged cells and frozen panes:** Merged regions are collapsed to their anchor value and annotated; frozen header rows are marked so they survive downstream truncation.
- **Empty sheet detection:** Sheets with no data are skipped and reported in the output metadata rather than emitted as empty sections.
- **Metadata:** Workbook properties (title, author, creation date) plus per-sheet statistics (rows, columns, used range).

## Configuration

This node has no configuration fields.

Its configuration shows the shared execution settings, but this node does not apply retries (`retryOnFail`, `maxTries`) yet. `continueOnFail` is not applied by any node.

## Output ports

- `extracted` (default): Extracted
- `empty`: Empty
- `error`: Error

## Input contract

- `content_base64` (string, optional)
- `file_path` (string, optional)

## Output contract

- `text` (string, optional)
- `chars` (number, optional)
- `empty` (boolean, optional)
- `method` (string, optional)
- `metadata` (object, optional)

## In expressions

What a later node can read from this node's output with `$("XLSX Text Extraction").item.json.<path>` (write your node's label instead of `XLSX Text Extraction`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `extracted`, from the output shown in Ports.

- `$("XLSX Text Extraction").item.json.text` (string) = `"## Sheet: Sales\n\n| Region | Total |\n| ---…"`
- `$("XLSX Text Extraction").item.json.chars` (number) = `84`
- `$("XLSX Text Extraction").item.json.empty` (boolean) = `false`
- `$("XLSX Text Extraction").item.json.method` (string) = `"openpyxl"`
- `$("XLSX Text Extraction").item.json.metadata` (object) = `{…}`
- `$("XLSX Text Extraction").item.json.metadata.sheets_total` (number) = `2` (Not suggested by the editor)
- `$("XLSX Text Extraction").item.json.metadata.sheets_with_data` (number) = `1` (Not suggested by the editor)
- `$("XLSX Text Extraction").item.json.metadata.sheet_names` (array) = `[…] (2)` (Not suggested by the editor)


## How it works

Reads a .xlsx workbook with openpyxl and renders every sheet that has data as a Markdown table under a `## Sheet: <name>` heading.

1. Resolves the node config: expressions in config keys are evaluated against the run; the incoming item is used as-is.
2. Reads `content_base64` or `file_path` from the config first, then from the incoming item. If neither is present it returns `error`.
3. Opens the workbook in values mode: formula cells give the value cached in the file, not the formula.
4. For each sheet in workbook order, drops fully empty rows, pads rows to the widest one, uses the first remaining row as the header and escapes `|` in cells.
5. Sheets with no data are skipped and only counted in `metadata`.
6. If the script fails it returns `error` with the Python message and stderr. Otherwise `empty: true` goes out through `empty` and any other result through `extracted`.

## In a workflow

### Node

```json
{
  "node_id": "node-xlsx-text",
  "label": "Read workbook",
  "operator_slug": "ai.xlsx.extract.text",
  "operator_version": "1.0.0",
  "config": {
    "execution": {
      "timeout_ms": 120000
    }
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "extracted",
  "data": {
    "text": "## Sheet: Sales\n\n| Region | Total |\n| --- | --- |\n| North | 1200 |\n| South | 980.5 |",
    "chars": 84,
    "empty": false,
    "method": "openpyxl",
    "metadata": {
      "sheets_total": 2,
      "sheets_with_data": 1,
      "sheet_names": [
        "Sales",
        "Notes"
      ]
    }
  }
}
```

## Output ports (behaviour in the code)

### `extracted`

At least one sheet has data.

```json
{
  "text": "## Sheet: Sales\n\n| Region | Total |\n| --- | --- |\n| North | 1200 |\n| South | 980.5 |",
  "chars": 84,
  "empty": false,
  "method": "openpyxl",
  "metadata": {
    "sheets_total": 2,
    "sheets_with_data": 1,
    "sheet_names": [
      "Sales",
      "Notes"
    ]
  }
}
```

### `empty`

No sheet has data. Same shape with `text: ""` and `empty: true`.

```json
{
  "text": "",
  "chars": 0,
  "empty": true,
  "method": "openpyxl",
  "metadata": {
    "sheets_total": 1,
    "sheets_with_data": 0,
    "sheet_names": [
      "Sheet1"
    ]
  }
}
```

### `error`

The input is missing or the script fails. `data` is `{ error }` or `{ error, stderr }`.

```json
{
  "error": "File is not a zip file",
  "stderr": ""
}
```

## Behaviour in the code

- `execution.timeout_ms`: Timeout of the Python subprocess. When unset the code uses 120000 ms (the schema shows 300000 as the default).
- `content_base64 / file_path`: Not declared in the config schema, but read from the config before the incoming item. Use them to point at a file with an expression, e.g. `{{ $json.attachment.content_base64 }}`.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `Either content_base64 or file_path is required.` | `error` | Neither the config nor the incoming item has `content_base64` or `file_path`. Map the file from the previous node. |
| `Either content_base64 or file_path is required` | `error` | Raised by the Python script when `file_path` does not exist on the orchestrator host. Data also carries `stderr`. |
| `<exception message from the Python library>` | `error` | The file could not be read as a .xlsx workbook (corrupt, encrypted or another format). `data.stderr` has the details. |
| `Python timed out after <timeout_ms>ms` | `error` | The script exceeded `execution.timeout_ms`. Raise it or process fewer pages. |
| `Python runtime not available (ENOENT)` | `error` | No Python interpreter is available on the orchestrator host. |

## Notes and limits

- Cell values are written as plain Python strings, not with Excel display formats: a date comes out as `2026-01-15 00:00:00` and 25% as `0.25`.
- Formulas are never computed. A workbook saved without cached values (for example, generated by a script) yields empty cells for its formulas.
- Not implemented, although the description mentions them: merged-cell and frozen-pane annotations, and workbook properties (title, author, creation date).
- Only .xlsx is supported; legacy .xls files fail.
- `file_path` is a path on the orchestrator host. `content_base64` wins when both are present.
- The extraction runs in a Python subprocess. On first use the orchestrator creates a virtual environment and installs the pinned requirements, which can take minutes; later runs reuse it.
- The Python result is limited to 50 MB of output; above it the node leaves through `error`.
- The engine does not apply `execution.retryOnFail`, `maxTries`, `waitBetweenTries` or `continueOnFail` to this node. Handle failures by wiring the `error` port.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "ai.xlsx.extract.text",
  "name": "XLSX Text Extraction",
  "category": "ai",
  "version": "1.0.0",
  "description": "Extracts tabular data from Microsoft Excel (.xlsx) workbooks using openpyxl.\n\n  Iterates every non-empty sheet in workbook order and renders each as a standalone markdown table, producing a single normalized text output suitable for downstream LLM consumption, vector indexing, or direct classification.\n\n  **Features:**\n  - **Per-sheet rendering:** Each sheet is emitted with its name as a header followed by a markdown table of its used range.\n  - **Formatted cell values:** Applies the workbook's display formats for numbers, dates, and percentages so the extracted text matches what a user sees in Excel.\n  - **Merged cells and frozen panes:** Merged regions are collapsed to their anchor value and annotated; frozen header rows are marked so they survive downstream truncation.\n  - **Empty sheet detection:** Sheets with no data are skipped and reported in the output metadata rather than emitted as empty sections.\n  - **Metadata:** Workbook properties (title, author, creation date) plus per-sheet statistics (rows, columns, used range).",
  "capabilities": [
    "xlsx",
    "office",
    "excel",
    "spreadsheet",
    "text-extraction"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "content_base64": {
        "type": "string"
      },
      "file_path": {
        "type": "string"
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "text": {
        "type": "string"
      },
      "chars": {
        "type": "number"
      },
      "empty": {
        "type": "boolean"
      },
      "method": {
        "type": "string"
      },
      "metadata": {
        "type": "object"
      }
    }
  },
  "ports": [
    {
      "port_id": "extracted",
      "label": "Extracted",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "empty",
      "label": "Empty",
      "is_default": false,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "tags": [
    "ai",
    "xlsx",
    "office",
    "excel",
    "spreadsheet",
    "text"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://me.veripass.com/sommatic",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  }
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "content_base64": {
      "type": "string"
    },
    "file_path": {
      "type": "string"
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "text": {
      "type": "string"
    },
    "chars": {
      "type": "number"
    },
    "empty": {
      "type": "boolean"
    },
    "method": {
      "type": "string"
    },
    "metadata": {
      "type": "object"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "extracted",
    "label": "Extracted",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "empty",
    "label": "Empty",
    "is_default": false,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "is_default": false,
    "supports_condition": false
  }
]
```
