# PPTX Text Extraction

> 借助 python-pptx 遍历 Office Open XML Presentation 结构，从 Microsoft PowerPoint（.pptx）演示文稿中抽取文本。

Source: https://docs.sommatic.ai/zh/nodes/ai.pptx.extract.text/

- 标识: `ai.pptx.extract.text`
- 类别: AI
- 版本: 1.0.0
- 状态: 可用
- 执行方式: async

借助 python-pptx 遍历 Office Open XML Presentation 结构，从 Microsoft PowerPoint（.pptx）演示文稿中抽取文本。

逐张幻灯片输出内容，保留演示文稿的顺序，并将幻灯片上的可见文本与演讲者备注分开。

**功能特性：**
- **保留幻灯片顺序：** 每张幻灯片生成一个分节标记，包含幻灯片编号、标题占位符，以及按阅读顺序拼接的正文占位符。
- **演讲者备注：** 按幻灯片抽取演讲者备注，并作为独立块输出，便于单独使用或过滤。
- **幻灯片中的表格：** 幻灯片内的表格渲染为 markdown 表格。
- **形状遍历：** 组合形状、文本框和 SmartArt 中的文本以确定性方式展平，同一份演示文稿始终得到相同输出。
- **隐藏幻灯片：** 标记为隐藏的幻灯片会在元数据中报告，但默认不纳入主文本流。

## 配置

该节点没有配置项。

配置中会显示通用执行设置，但该节点目前不执行重试（`retryOnFail`、`maxTries`）。所有节点均未实现 `continueOnFail`。

## 输出端口

- `extracted` (默认): Extracted
- `empty`: Empty
- `error`: Error

## 输入契约

- `content_base64` (string, 可选)
- `file_path` (string, 可选)

## 输出契约

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

## 在表达式中

后续节点可通过 `$("PPTX Text Extraction").item.json.<路径>` 读取此节点输出中的内容（请把 `PPTX Text Extraction` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `extracted` 返回的路径，依据“端口”一节所示的输出。

- `$("PPTX Text Extraction").item.json.text` (string) = `"## Slide 1\n\nQuarterly review\n\nRevenue gr…"`
- `$("PPTX Text Extraction").item.json.chars` (number) = `106`
- `$("PPTX Text Extraction").item.json.empty` (boolean) = `false`
- `$("PPTX Text Extraction").item.json.method` (string) = `"python-pptx"`
- `$("PPTX Text Extraction").item.json.metadata` (object) = `{…}`
- `$("PPTX Text Extraction").item.json.metadata.slides_total` (number) = `3` (编辑器不提示)
- `$("PPTX Text Extraction").item.json.metadata.slides_with_content` (number) = `2` (编辑器不提示)


## 工作原理

使用 python-pptx 提取 .pptx 演示文稿的文本：每张幻灯片一个 `## Slide n` 块，包含其文本框和占位符中的文本，随后是演讲者备注。

1. 解析节点配置：配置键中的表达式会基于本次运行求值；传入条目按原样使用。
2. 先从配置、再从传入条目读取 `content_base64` 或 `file_path`。两者都没有时返回 `error`。
3. 打开演示文稿并按顺序遍历幻灯片。对每张幻灯片，保留所有含文本框架的顶层形状中的文本，每段一行。
4. 将演讲者备注以 `**Notes:** …` 的形式追加到该幻灯片块中。既无文本也无备注的幻灯片会被省略。
5. 脚本失败时返回 `error`，附带 Python 消息和 stderr。否则 `empty: true` 从 `empty` 输出，其他结果从 `extracted` 输出。

## 在工作流中

### 节点

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

### Envelope

```json
{
  "ok": true,
  "port": "extracted",
  "data": {
    "text": "## Slide 1\n\nQuarterly review\n\nRevenue grew 12%\n\n**Notes:** Mention the new region.\n\n## Slide 2\n\nNext steps",
    "chars": 106,
    "empty": false,
    "method": "python-pptx",
    "metadata": {
      "slides_total": 3,
      "slides_with_content": 2
    }
  }
}
```

## 输出端口 (代码中的行为)

### `extracted`

至少有一张幻灯片含有文本或备注。

```json
{
  "text": "## Slide 1\n\nQuarterly review\n\nRevenue grew 12%\n\n**Notes:** Mention the new region.\n\n## Slide 2\n\nNext steps",
  "chars": 106,
  "empty": false,
  "method": "python-pptx",
  "metadata": {
    "slides_total": 3,
    "slides_with_content": 2
  }
}
```

### `empty`

没有任何幻灯片包含可读文本或备注。结构相同，`text: ""` 且 `empty: true`。

```json
{
  "text": "",
  "chars": 0,
  "empty": true,
  "method": "python-pptx",
  "metadata": {
    "slides_total": 4,
    "slides_with_content": 0
  }
}
```

### `error`

缺少输入或脚本失败。`data` 为 `{ error }` 或 `{ error, stderr }`。

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

## 代码中的行为

- `execution.timeout_ms`: Python 子进程的超时时间。未设置时代码使用 120000 毫秒（schema 显示的默认值为 300000）。
- `content_base64 / file_path`: 它们未在配置 schema 中声明，但会先于传入条目从配置读取。可用表达式指向文件，例如 `{{ $json.attachment.content_base64 }}`。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `Either content_base64 or file_path is required.` | `error` | 配置和传入条目中都没有 `content_base64` 或 `file_path`。请从上一个节点映射文件。 |
| `Either content_base64 or file_path is required` | `error` | 当 `file_path` 在编排器主机上不存在时由 Python 脚本抛出。数据中还包含 `stderr`。 |
| `<exception message from the Python library>` | `error` | 无法将文件读取为.pptx 演示文稿（文件损坏、已加密或为其他格式）。详情见 `data.stderr`。 |
| `Python timed out after <timeout_ms>ms` | `error` | 脚本超过了 `execution.timeout_ms`。请调大该值或减少处理的页数。 |
| `Python runtime not available (ENOENT)` | `error` | 编排器主机上没有可用的 Python 解释器。 |

## 说明与限制

- 只读取含文本框架的顶层形状。表格、组合形状、图表和 SmartArt 会被跳过，尽管描述中另有说法。
- 不会识别隐藏幻灯片：它们与其他幻灯片一样被提取。备注位于各幻灯片块内，而不是单独字段。
- 仅支持 Open XML 格式（.pptx）；旧版 .ppt 文件会失败。
- `file_path` 是编排器主机上的路径。两者同时存在时以 `content_base64` 为准。
- 提取在 Python 子进程中运行。首次使用时，编排器会创建虚拟环境并安装固定版本的依赖，可能需要数分钟；之后的运行会复用该环境。
- Python 结果的输出上限为 50 MB；超过后节点从 `error` 端口输出。
- 引擎不会对此节点应用 `execution.retryOnFail`、`maxTries`、`waitBetweenTries` 或 `continueOnFail`。请通过连接 `error` 端口处理失败。


## 契约（JSON Schema）

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

```json
{
  "slug": "ai.pptx.extract.text",
  "name": "PPTX Text Extraction",
  "category": "ai",
  "version": "1.0.0",
  "description": "Extracts text from Microsoft PowerPoint (.pptx) presentations by traversing the Office Open XML Presentation structure with python-pptx.\n\n  Emits slide-by-slide content preserving deck order and separating visible slide text from presenter notes.\n\n  **Features:**\n  - **Slide order preservation:** Every slide produces a section marker with its slide number, title placeholder, and body placeholders concatenated in reading order.\n  - **Presenter notes:** Speaker notes are extracted per slide and emitted as a separate block so they can be used or filtered independently.\n  - **Tables within slides:** In-slide tables are rendered as markdown tables.\n  - **Shape traversal:** Text from grouped shapes, text frames, and SmartArt is flattened deterministically so the same deck always yields the same output.\n  - **Hidden slides:** Slides flagged as hidden are reported in metadata but excluded from the main text stream by default.",
  "capabilities": [
    "pptx",
    "office",
    "powerpoint",
    "presentation",
    "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",
    "pptx",
    "office",
    "powerpoint",
    "presentation",
    "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
  }
]
```
