Skip to content

与格式无关的IR

DocumentIR是Office Oxide跨格式的结构桥梁。无论你打开.docx.xlsx还是旧版的.ppt,得到的都是同样的结构:一组section,每个section内包含一系列带类型的元素(标题、段落、表格、列表、图片)。

IR支撑了to_htmlsave_as和旧版→OOXML转换。它也是下游管道(搜索索引、RAG分块器、自定义渲染器)最合适的接口 — 因为你只需处理一个schema,而非六个各异的格式专属schema。

读取IR

Rust

use office_oxide::Document;

let doc = Document::open("report.docx")?;
let ir = doc.to_ir();

for section in &ir.sections {
    println!("{:?}", section.title);
    for el in &section.elements {
        // el 是 Element 枚举 — Heading、Paragraph、Table、List、Image 等
    }
}

Python

from office_oxide import Document

with Document.open("report.docx") as doc:
    ir = doc.to_ir()

for section in ir["sections"]:
    print(section.get("title"))
    for el in section["elements"]:
        kind = el["kind"]   # "Heading" | "Paragraph" | "Table" | "List" | "Image"

JavaScript

using doc = Document.open('report.docx');
const ir = doc.toIr();

for (const section of ir.sections) {
  for (const el of section.elements) {
    // el.kind: "Heading" | "Paragraph" | "Table" | "List" | "Image"
  }
}

Go

import "encoding/json"

irJSON, _ := doc.ToIRJSON()

var ir struct {
    Sections []struct {
        Title    *string           `json:"title"`
        Elements []json.RawMessage `json:"elements"`
    } `json:"sections"`
}
_ = json.Unmarshal([]byte(irJSON), &ir)

C#

using System.Text.Json;

using var doc = Document.Open("report.docx");
using var ir = JsonDocument.Parse(doc.ToIrJson());

foreach (var section in ir.RootElement.GetProperty("sections").EnumerateArray())
{
    // ...
}

C

int err = 0;
OfficeDocumentHandle *doc = office_document_open("report.docx", &err);
char *ir_json = office_document_to_ir_json(doc, &err);   /* DocumentIR as JSON */
if (ir_json) {
    /* parse with your JSON lib — sections[].elements[].kind: "Heading" | ... */
    office_oxide_free_string(ir_json);
}
office_document_free(doc);

WASM

import { WasmDocument } from 'office-oxide-wasm';

const data = new Uint8Array(await (await fetch('/report.docx')).arrayBuffer());
const doc = new WasmDocument(data, 'docx');
try {
  const ir = doc.toIr();                 // JS object, schema == Rust DocumentIR
  for (const section of ir.sections) {
    for (const el of section.elements) {
      // el.kind: "Heading" | "Paragraph" | "Table" | "List" | "Image"
    }
  }
} finally {
  doc.free();
}

Schema

结构有意保持精简且稳定。

{
  "sections": [
    {
      "title": "Optional section title",     // string | null
      "elements": [
        { "kind": "Heading", "level": 1, "text": "..." },
        { "kind": "Paragraph", "runs": [
            { "text": "Hello ", "bold": false, "italic": false },
            { "text": "world", "bold": true,  "italic": false }
        ] },
        { "kind": "List", "ordered": true, "items": ["one", "two"] },
        { "kind": "Table", "rows": [
            ["A1", "B1"],
            ["A2", "B2"]
        ] },
        { "kind": "Image", "filename": "image1.png", "data": "<base64>" }
      ]
    }
  ]
}

各格式的映射:

格式 section边界 备注
DOCX 每个<w:sectPr>一个section(没有则整篇body算一个) 标题按w:pStyle识别
XLSX 每个工作表一个section title = 工作表名;每个使用区域一个Table元素
PPTX 每张幻灯片一个section title = 标题占位符;备注作为最后一个段落附上
DOC / XLS / PPT 与对应OOXML形态相同 走旧版CFB解析管线

为什么使用IR

  • 构建一次,多端渲染。 把DOCX、XLSX、PPTX统一转成同一结构,跑同一个搜索/分块管线。
  • 跨格式变迁的LLM上下文。 源文件从.doc迁移到.docx时schema不会漂移。
  • 通过save_as实现往返编辑。 修改IR后,写出任何受支持格式的新文档。

Rust

use office_oxide::create::create_from_ir;
use office_oxide::DocumentFormat;

create_from_ir(&ir, DocumentFormat::Docx, "out.docx")?;

序列化

Rust的DocumentIR通过serde派生了Serialize / Deserialize。Python的to_ir()返回普通dict(已可直接JSON序列化)。Node、Go、C#和C绑定通过to_ir_json() / ToIRJSON() / ToIrJson()暴露JSON字符串。

相关链接