Формат-незалежний 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 §ion.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())
{
// ...
}
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();
}
Схема
Форма навмисно компактна й стабільна.
{
"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>" }
]
}
]
}
Маппінг за форматами:
| Формат | Межа секції | Примітки |
|---|---|---|
| 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 — запишіть новий документ у будь-якому підтримуваному форматі.
Rust
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().
Дивіться також
- Видобування Markdown — текст у форматі, зручному для LLM
- Видобування HTML — стилізований вивід
- Конвертація: legacy→OOXML — під капотом використовує IR