Office ドキュメントを HTML に変換する
Office Oxide のすべてのハンドルには to_html() メソッドがあり、サポートされている任意のフォーマットからクリーンなセマンティック HTML5 を出力します。ブラウザプレビュー、メール描画、ビジュアル差分確認など幅広い用途に使えます。
ワンショット変換
Rust
use office_oxide::to_html;
let html = to_html("report.docx")?;
std::fs::write("report.html", html)?;
Python
import office_oxide
html = office_oxide.to_html("report.docx")
open("report.html", "w").write(html)
JavaScript
import { toHtml } from 'office-oxide';
import { writeFileSync } from 'node:fs';
writeFileSync('report.html', toHtml('report.docx'));
Go
html, err := officeoxide.ToHTML("report.docx")
os.WriteFile("report.html", []byte(html), 0o644)
C#
File.WriteAllText("report.html", OfficeOxide.ToHtml("report.docx"));
C
int err = 0;
char *html = office_to_html("report.docx", &err); /* HTML fragment */
if (html) {
FILE *f = fopen("report.html", "w");
fputs(html, f);
fclose(f);
office_oxide_free_string(html);
}
再利用可能なハンドル
Rust
let doc = office_oxide::Document::open("slides.pptx")?;
let html = doc.to_html();
Python
from office_oxide import Document
with Document.open("slides.pptx") as doc:
html = doc.to_html()
JavaScript
using doc = Document.open('slides.pptx');
const html = doc.toHtml();
C
int err = 0;
OfficeDocumentHandle *doc = office_document_open("slides.pptx", &err);
char *html = office_document_to_html(doc, &err); /* HTML fragment */
if (html) { /* use html */ office_oxide_free_string(html); }
office_document_free(doc);
WASM
import { WasmDocument } from 'office-oxide-wasm';
const data = new Uint8Array(await (await fetch('/slides.pptx')).arrayBuffer());
using doc = new WasmDocument(data, 'pptx');
const html = doc.toHtml(); // HTML fragment
出力される内容
HTML は フラグメント形式 — <html>、<head>、<body> の外枠ラッパーは含まれません。マウントする場所やスタイルシートはご自身で決定できます。
| ソース | HTML 要素 |
|---|---|
| 見出し | <h1> … <h6>(ソースのレベルに対応) |
| 段落 | <p> |
| 太字 / 斜体 / 下線 | <strong>、<em>、<u> |
| リスト | <ul> / <ol> と <li> の子要素 |
| テーブル | <table>(<thead>、<tbody>、<tr>、<th>、<td> を含む) |
| ハイパーリンク | <a href="..."> |
| 画像 | <img src="..." alt="..."> |
| XLSX シート | <section data-sheet="name"> + <table> |
| PPTX スライド | <section data-slide="N"> + 本文 + ノート用 <aside>(省略可) |
出力はエスケープ処理済みです。ドキュメント内のユーザーコンテンツはすべて HTML エスケープされているため、ページへの埋め込みはデフォルトで安全です。
スタンドアロンページとしてラップする
Python
from office_oxide import Document
with Document.open("report.docx") as doc:
body = doc.to_html()
page = f"""<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>Report</title>
<link rel="stylesheet" href="docs.css">
</head><body>{body}</body></html>"""
open("report.html", "w").write(page)
主なユースケース
- アップロードされたドキュメントのブラウザ内プレビュー(
<input type="file">→ WASM →<iframe srcdoc>) - 生成されたレポートのメール描画
- 差分表示 — HTML 差分はコードレビューツールで意味のある形で表示される
- 検索インデックス — 構造を保持したままなので、見出しで検索結果をブーストできる
関連ドキュメント
- Markdown 抽出 — プレーンテキスト寄りの出力が必要な場合
- IR 抽出 — 独自レンダリング向けの構造化 JSON
- WASM クイックスタート — ブラウザ内変換の始め方