Skip to content

Formatunabhängige IR

DocumentIR ist die strukturelle Brücke von Office Oxide zwischen den Formaten. Egal ob du eine .docx, .xlsx oder ein Legacy-.ppt öffnest — du bekommst dieselbe Form zurück: eine Liste Abschnitte, jeder mit einer Sequenz typisierter Elemente (Überschriften, Absätze, Tabellen, Listen, Bilder).

Die IR trägt to_html, save_as und die Legacy → OOXML-Konvertierung. Sie ist auch die richtige Schnittstelle für Downstream-Pipelines — Suchindizes, RAG-Chunker, eigene Renderer — weil du ein Schema verarbeitest statt sechs formatspezifische.

IR lesen

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 ist ein Element-Enum — 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())
{
    // ...
}

Schema

Die Form ist bewusst klein und stabil gehalten.

{
  "sections": [
    {
      "title": "Optionaler Abschnittstitel",     // 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>" }
      ]
    }
  ]
}

Mapping pro Format:

Format Abschnittsgrenze Anmerkungen
DOCX Ein Abschnitt pro <w:sectPr> (oder ganzer Body, wenn keiner da) Überschriften per w:pStyle zugeordnet
XLSX Ein Abschnitt pro Worksheet title = Sheet-Name; ein Table-Element pro genutztem Range
PPTX Ein Abschnitt pro Folie title = Folientitel-Placeholder; Notizen als letzter Absatz angehängt
DOC / XLS / PPT Gleiche Form wie ihre OOXML-Gegenstücke Geparst über die Legacy-CFB-Pipeline

Warum die IR nutzen

  • Einmal bauen, vielfach rendern. Konvertiere DOCX, XLSX und PPTX in dieselbe Form, fahre eine einzige Such-/Chunk-Pipeline.
  • LLM-Kontext, der Formatwechsel überlebt. Das Schema driftet nicht, wenn Dokumente von .doc nach .docx wandern.
  • Round-Trip mit save_as. Bearbeite die IR und schreibe ein neues Dokument in jedem unterstützten Format.
use office_oxide::create::create_from_ir;
use office_oxide::DocumentFormat;

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

Serialisierung

Die Rust-DocumentIR deriviert Serialize / Deserialize (via serde). Pythons to_ir() liefert ein normales dict zurück (bereits JSON-serialisierbar). Die Node-, Go-, C#- und C-Bindings stellen JSON-Strings über to_ir_json() / ToIRJSON() / ToIrJson() bereit.

Siehe auch