从 IR 创建文档
create_from_ir 从 DocumentIR 写出全新的 DOCX、XLSX 或 PPTX — 也就是你从 to_ir() 拿到的同一结构化 schema。一个 IR、三种目标格式。IR 是 LLM 生成内容的天然落点:让模型产出结构化 JSON,需要时再把它实体化成对应的 Office 格式。
构建并写出
Rust
use office_oxide::{DocumentFormat};
use office_oxide::create::create_from_ir;
use office_oxide::ir::{DocumentIR, Section, Element, Run};
let ir = DocumentIR {
sections: vec![Section {
title: Some("季度报告".into()),
elements: vec![
Element::Heading { level: 1, text: "亮点".into() },
Element::Paragraph { runs: vec![Run::plain("收入增长 18%。")] },
Element::Table {
rows: vec![
vec!["区域".into(), "收入".into()],
vec!["NA".into(), "$1.2M".into()],
vec!["EU".into(), "$820K".into()],
],
},
],
}],
};
create_from_ir(&ir, DocumentFormat::Docx, "report.docx")?;
切换目标格式即可从同一 IR 得到 XLSX 或 PPTX:
create_from_ir(&ir, DocumentFormat::Xlsx, "report.xlsx")?;
create_from_ir(&ir, DocumentFormat::Pptx, "report.pptx")?;
Python
from office_oxide import create_from_ir
ir = {
"sections": [{
"title": "季度报告",
"elements": [
{"kind": "Heading", "level": 1, "text": "亮点"},
{"kind": "Paragraph", "runs": [{"text": "收入增长 18%。"}]},
{"kind": "Table", "rows": [
["区域", "收入"],
["NA", "$1.2M"],
["EU", "$820K"],
]},
],
}],
}
create_from_ir(ir, "docx", "report.docx")
create_from_ir(ir, "xlsx", "report.xlsx")
create_from_ir(ir, "pptx", "report.pptx")
JavaScript
import { createFromIr } from 'office-oxide';
const ir = {
sections: [{
title: 'Quarterly Report',
elements: [
{ kind: 'Heading', level: 1, text: 'Highlights' },
{ kind: 'Paragraph', runs: [{ text: 'Revenue grew 18%.' }] },
{ kind: 'Table', rows: [
['Region', 'Revenue'],
['NA', '$1.2M'],
['EU', '$820K'],
]},
],
}],
};
createFromIr(ir, 'docx', 'report.docx');
createFromIr(ir, 'xlsx', 'report.xlsx');
createFromIr(ir, 'pptx', 'report.pptx');
Go
Go 以 JSON 负载形式接收 IR:
import (
"encoding/json"
officeoxide "github.com/yfedoseev/office_oxide/go"
)
ir := map[string]any{
"sections": []any{
map[string]any{
"title": "Quarterly Report",
"elements": []any{
map[string]any{"kind": "Heading", "level": 1, "text": "Highlights"},
map[string]any{"kind": "Paragraph", "runs": []any{map[string]any{"text": "Revenue grew 18%."}}},
map[string]any{"kind": "Table", "rows": [][]string{
{"Region", "Revenue"},
{"NA", "$1.2M"},
{"EU", "$820K"},
}},
},
},
},
}
payload, _ := json.Marshal(ir)
officeoxide.CreateFromIR(string(payload), "docx", "report.docx")
officeoxide.CreateFromIR(string(payload), "xlsx", "report.xlsx")
officeoxide.CreateFromIR(string(payload), "pptx", "report.pptx")
C#
using OfficeOxide;
using System.Text.Json;
var ir = new {
sections = new[] {
new {
title = "Quarterly Report",
elements = new object[] {
new { kind = "Heading", level = 1, text = "Highlights" },
new { kind = "Paragraph", runs = new[] { new { text = "Revenue grew 18%." } } },
new { kind = "Table", rows = new[] {
new[] { "Region", "Revenue" },
new[] { "NA", "$1.2M" },
new[] { "EU", "$820K" },
}},
},
},
},
};
var payload = JsonSerializer.Serialize(ir);
OfficeOxide.CreateFromIr(payload, "docx", "report.docx");
OfficeOxide.CreateFromIr(payload, "xlsx", "report.xlsx");
OfficeOxide.CreateFromIr(payload, "pptx", "report.pptx");
各目标格式如何渲染 IR
| IR 元素 | DOCX | XLSX | PPTX |
|---|---|---|---|
Section |
Word 节分隔符 | 工作表(一个 section → 一张工作表) | 一张幻灯片 |
Heading { level } |
带 Heading{level} 样式的段落 |
行首加粗单元格 | 幻灯片标题占位符 |
Paragraph |
正文段落 | 连续行的单元格 | 正文文本占位符 |
List { ordered, items } |
编号或项目符号列表 | A 列单元格 | 正文项目符号 |
Table { rows } |
Word 表 | 从下一空行起的单元格 | PowerPoint 表 |
Image { filename, data } |
内联图片 | (跳过 — XLSX 需要锚点坐标) | 幻灯片图片 |
映射偏保守:每个 IR 元素都产生确定输出,但每种格式都有 IR 没建模的特性(XLSX 合并、PPTX 动画、DOCX 评论线程)。要更丰富的输出请用按格式构建器。
来回往返 — 读、改、写
把 to_ir() 和 create_from_ir 结合,可以做跨格式存活的结构化编辑:
from office_oxide import Document, create_from_ir
with Document.open("legacy.doc") as doc:
ir = doc.to_ir()
# 添加一个新 section
ir["sections"].append({
"title": "附录",
"elements": [
{"kind": "Heading", "level": 2, "text": "更新条款"},
{"kind": "Paragraph", "runs": [{"text": "自 2026-04-19 起生效。"}]},
],
})
create_from_ir(ir, "docx", "modernized.docx")
这也是做旧版 → OOXML 转换(DOC → DOCX、XLS → XLSX、PPT → PPTX)最干净的方式。参见 转换:旧版 → OOXML。
为何用 IR 来创建
- 格式无关的模板。同一份内容既能给 Word 用户出 DOCX、给分析师出 XLSX,也能给演示出 PPTX,无需重写。
- 原生贴合 LLM。让模型产出符合 IR schema 的 JSON,再实体化文件。比直接让它输出 Office XML 可靠得多。
- 可 diff。IR 是 JSON,可以提交版本管理。Office 文件成为构建产物。
限制
Element::Image中的图片在目标格式支持内联图片时被写入(DOCX、PPTX)。XLSX 需要锚点坐标,简单 IR 不带 — 跳过或使用按格式构建器。- 单元格格式(数字格式、货币、加粗)不属于 IR。创建后通过按格式编辑器添加。
- 图表、SmartArt 与嵌入对象不属于 IR。
任何超出 IR 表达范围的,请用按格式构建器(docx::create::DocxBuilder、xlsx::create::XlsxBuilder、pptx::create::PptxBuilder)。它们用「绑定到具体格式」换来对 OOXML 特性的完整访问。
相关链接
- 结构化 IR — 详细 schema
- 转换:旧版 → OOXML — 底层即用
create_from_ir - 编辑概览 — 就地修改既有文件