Office ドキュメントを HTML に変換
Office Oxide のすべてのハンドルには to_html() メソッドがあり、サポートされている任意のフォーマットからクリーンでセマンティックな HTML5 を出力します。ブラウザプレビュー、メールレンダリング、簡単な視覚的差分などに利用できます。
ワンショット
Python
import office_oxide
html = office_oxide.to_html("report.docx")
open("report.html", "w").write(html)
Rust
use office_oxide::to_html;
let html = to_html("report.docx")?;
std::fs::write("report.html", 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"));
再利用可能なハンドル
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();
Rust
let doc = office_oxide::Document::open("slides.pptx")?;
let html = doc.to_html();
出力される内容
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="名前"> + <table> |
| PPTX スライド | <section data-slide="N"> + 本文 + ノート用のオプショナル <aside> |
出力はエスケープされます: ドキュメント内のユーザーコンテンツは HTML エスケープされるため、結果をページに埋め込んでも安全です。
スタンドアロンページとしてラップ
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>レポート</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 クイックスタート — ブラウザ内変換用