Skip to content

Формат-независимый IR

DocumentIR — структурный мост Office Oxide между форматами. Откройте .docx, .xlsx или legacy .ppt — и получите одну и ту же форму: список секций, в каждой из которых последовательность типизированных элементов (заголовки, абзацы, таблицы, списки, изображения).

IR двигает to_html, save_as и legacy → OOXML конвертацию. И это правильная поверхность для downstream-пайплайнов — поисковых индексов, RAG-чанкеров, своих рендереров — потому что вы обрабатываете одну схему вместо шести форматных.

Прочитать 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 — это enum 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())
{
    // ...
}

Схема

Форма намеренно компактная и стабильная.

{
  "sections": [
    {
      "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>" }
      ]
    }
  ]
}

Маппинг по форматам:

Формат Граница секции Заметки
DOCX По одной секции на <w:sectPr> (или весь body, если нет) Заголовки — по w:pStyle
XLSX По одной секции на лист title = имя листа; по одному Table на используемый диапазон
PPTX По одной секции на слайд title = плейсхолдер заголовка; заметки прицепляются последним абзацем
DOC / XLS / PPT Та же форма, что у их OOXML-аналогов Парсится через legacy CFB-пайплайн

Зачем использовать IR

  • Сделай раз, рендерь по-разному. Сконвертируйте DOCX, XLSX и PPTX в одну форму, гоняйте единый пайплайн поиска/чанкинга.
  • LLM-контекст, переживающий смену формата. Схема не дрейфует, когда источники мигрируют с .doc на .docx.
  • Round-trip с save_as. Поправьте IR — запишите новый документ в любом поддерживаемом формате.
use office_oxide::create::create_from_ir;
use office_oxide::DocumentFormat;

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

Сериализация

Rust-DocumentIR имеет Serialize / Deserialize (через serde). Python-to_ir() возвращает обычный dict (уже JSON-сериализуемый). Node, Go, C# и C-привязки выставляют JSON-строки через to_ir_json() / ToIRJSON() / ToIrJson().

Смотрите также