Skip to content

IR からドキュメントを作成

create_from_irDocumentIR から DOCX、XLSX、PPTX を新規に書き出します — to_ir() から得られる同じ構造化スキーマです。1 つの IR、3 つのターゲットフォーマット。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 は IR を JSON ペイロードとして受け取ります:

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 のセクション区切り ワークシート(1 セクション → 1 シート) スライド
Heading { level } Heading{level} スタイルの段落 行頭の太字セル スライドタイトルプレースホルダ
Paragraph 本文段落 連続する行のセル 本文テキストプレースホルダ
List { ordered, items } 番号付き or 箇条書きリスト 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()

# 新しいセクションを追加
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)の最もクリーンな方法でもあります。Conversion: legacy → OOXML を参照。

なぜ作成に IR を使うのか

  • フォーマット非依存のテンプレート。Word ユーザー向けの DOCX、アナリスト向けの XLSX、プレゼン向けの PPTX として同じコンテンツを書き直しなしで生成。
  • LLM ネイティブ。モデルに IR スキーマに合致する JSON を生成させ、ファイルを実体化。Office XML を直接吐かせるよりはるかに信頼できます。
  • 差分可能。IR は JSON; コミット可能。Office ファイルはビルド成果物に。

制限

  • Element::Image の画像は、ターゲットフォーマットがインライン画像をサポートする場合(DOCX、PPTX)に書かれます。XLSX はアンカー座標が必要で、シンプルな IR には載っていません — スキップするか、フォーマット別ビルダーを使用。
  • セル書式(数値書式、通貨、太字)は IR の一部ではありません。作成後にフォーマット別エディタ経由で追加。
  • グラフ、SmartArt、埋め込みオブジェクトは IR の一部ではありません。

IR の表現範囲を超えるすべてには、フォーマット別ビルダー(docx::create::DocxBuilderxlsx::create::XlsxBuilderpptx::create::PptxBuilder)に降りてください。OOXML の機能フルアクセスと引き換えにフォーマット固有になります。

関連項目