Skip to content

Office ドキュメントを Markdown に変換する

Office Oxide のすべてのハンドルには to_markdown() メソッドがあり、サポートされている 6 つのフォーマットいずれからでも GitHub Flavored Markdown(見出し・テーブル・リスト・コードブロック)を生成します。LLM や RAG パイプラインのエントリーポイントとして最適です。

ワンショット

Rust

use office_oxide::to_markdown;

let md = to_markdown("report.docx")?;
std::fs::write("report.md", md)?;

Python

import office_oxide

md = office_oxide.to_markdown("report.docx")
open("report.md", "w").write(md)

JavaScript

import { toMarkdown } from 'office-oxide';
import { writeFileSync } from 'node:fs';

writeFileSync('report.md', toMarkdown('report.docx'));

Go

md, err := officeoxide.ToMarkdown("report.docx")
os.WriteFile("report.md", []byte(md), 0o644)

C#

File.WriteAllText("report.md", OfficeOxide.ToMarkdown("report.docx"));

C

int err = 0;
char *md = office_to_markdown("report.docx", &err);   /* open + render in one call */
if (md) {
    FILE *f = fopen("report.md", "w");
    fputs(md, f);
    fclose(f);
    office_oxide_free_string(md);
}

ハンドルの再利用

Rust

let doc = office_oxide::Document::open("deck.pptx")?;
let md = doc.to_markdown();

Python

from office_oxide import Document

with Document.open("deck.pptx") as doc:
    md = doc.to_markdown()

JavaScript

using doc = Document.open('deck.pptx');
const md = doc.toMarkdown();

C

int err = 0;
OfficeDocumentHandle *doc = office_document_open("deck.pptx", &err);
if (doc) {
    char *md = office_document_to_markdown(doc, &err);
    if (md) { /* use md */ office_oxide_free_string(md); }
    office_document_free(doc);
}

WASM

import { WasmDocument } from 'office-oxide-wasm';

// WASM has no file I/O — read the bytes yourself, then open from bytes
const data = new Uint8Array(await (await fetch('/deck.pptx')).arrayBuffer());
using doc = new WasmDocument(data, 'pptx');
const md = doc.toMarkdown();

出力される要素

ソース要素 Markdown
DOCX 見出し(<w:pStyle w:val="Heading1"/> …) # Heading(スタイルに応じたレベル)
DOCX 段落 段落ひとつ、ソフトハイフンは除去
DOCX リスト項目 - item または 1. item(番号付きを維持)
DOCX テーブル GFM パイプテーブル
XLSX シート ## Sheet name に続いて範囲ごとのパイプテーブル
XLSX 結合セル 先頭セルの内容のみ、スパン情報は破棄
PPTX スライド ## Slide N + 本文、ノートはブロッククォートとして末尾に付加
PPTX テーブル スライド内にインラインで GFM パイプテーブル
ハイパーリンク [text](url)
画像 ![alt](filename) プレースホルダー — 下記「画像」を参照

画像

to_markdown() は画像をファイル名のプレースホルダー(例:![](image1.png))として出力しますが、画像のバイトデータは抽出しません。Markdown はテキスト形式だからです。画像を取り出すには IR またはフォーマット固有のアクセス方法を使用してください。

from office_oxide import Document

with Document.open("report.docx") as doc:
    ir = doc.to_ir()
    for section in ir["sections"]:
        for el in section["elements"]:
            if el["kind"] == "Image":
                print(el["filename"], len(el["data"]))

完全なスキーマは IR 抽出 を参照してください。

ユースケース

  • RAG インジェスト — Markdown は LLM にとって最も扱いやすい入力フォーマットです。ドキュメント 1 件につき 1 回のパス、決定論的な構造、HTML ノイズなし。
  • ドキュメントのインデックス化 — 見出しが自然なチャンク境界となり、テーブルはそのままクエリ可能です。
  • マイグレーション — 静的サイトジェネレーター(Hugo、Astro、MkDocs)向けに DOCX を Markdown へ変換。
  • コンテンツの差分確認 — Markdown の差分は .docx のバイナリ差分と比べてはるかに読みやすいです。

パフォーマンス

to_markdown()plain_text() と同程度の速度で動作します。中央値のドキュメントでは通常 1〜2 倍のコストです。詳しくは パフォーマンス をご覧ください。

関連ドキュメント

  • HTML 抽出 — スタイル付き出力が必要な場合
  • IR 抽出 — より高度なパイプライン向けの構造化 JSON
  • PDF for RAG — PDF を対象とするコンパニオンライブラリ pdf_oxide