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> |
| 목록 | <li> 자식을 가진 <ul> / <ol> |
| 표 | <thead>, <tbody>, <tr>, <th>, <td>를 포함한 <table> |
| 하이퍼링크 | <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 diff는 코드 리뷰 도구에서 의미 있게 표시됨
- 구조를 유지한 검색 인덱싱 (제목이 검색 결과 순위를 높일 수 있음)
더 보기
- Markdown 추출 — 일반 텍스트 친화적인 출력이 필요할 때
- IR 추출 — 직접 렌더링하기 위한 구조화 JSON
- WASM 빠른 시작 — 브라우저 내 변환용