Skip to content

Office ドキュメントからテキストを抽出する

Office Oxide はエントリポイントをひとつ提供します — extract_text()(または Document.open(...).plain_text())— DOCX・XLSX・PPTX・DOC・XLS・PPT のすべてで同じ使い方ができます。フォーマットごとに処理を分ける必要はありません。

ワンショットヘルパー

最速の方法: ファイルを開き、フォーマットに適した抽出器を実行して文字列を返す、単一の関数呼び出しです。

Rust

use office_oxide::extract_text;

let text = extract_text("report.docx")?;
println!("{text}");

Python

import office_oxide

text = office_oxide.extract_text("report.docx")
print(text)

JavaScript

import { extractText } from 'office-oxide';

console.log(extractText('report.docx'));

Go

import officeoxide "github.com/yfedoseev/office_oxide/go"

text, err := officeoxide.ExtractText("report.docx")

C#

using OfficeOxide;

string text = OfficeOxide.ExtractText("report.docx");

C

int err = 0;
char *text = office_extract_text("report.docx", &err);
if (text) {
    printf("%s\n", text);
    office_oxide_free_string(text);
}

再利用可能なハンドル

テキストに加えて Markdown・HTML・IR などの出力も必要な場合は、ドキュメントを一度開いてハンドルを使い回してください。

Rust

use office_oxide::Document;

let doc = Document::open("report.docx")?;
let text = doc.plain_text();
let md   = doc.to_markdown();

Python

from office_oxide import Document

with Document.open("report.docx") as doc:
    text = doc.plain_text()
    md   = doc.to_markdown()

JavaScript

import { Document } from 'office-oxide';

using doc = Document.open('report.docx');
const text = doc.plainText();
const md   = doc.toMarkdown();

C

int err = 0;
OfficeDocumentHandle *doc = office_document_open("report.docx", &err);
if (!doc) { fprintf(stderr, "open failed: code=%d\n", err); return 1; }

char *text = office_document_plain_text(doc, &err);
char *md   = office_document_to_markdown(doc, &err);
/* use text / md ... */
office_oxide_free_string(text);
office_oxide_free_string(md);
office_document_free(doc);

フォーマット別の出力内容

フォーマット 出力内容
DOCX 文書順のボディテキスト(ヘッダー・フッター含む)、ソフトハイフン除去済み
XLSX 全シートのセル値(行内はタブ区切り、シート間は空行で区切り)
PPTX スライドタイトル・ボディプレースホルダー・テーブルセル・ノート(段落ブロック単位で 1 スライド)
DOC DOCX と同じ形式、CFB ピーステーブルから直接解析
XLS XLSX と同じ形式、BIFF8 レコードから直接解析
PPT PPTX と同じ形式、PowerPoint Document ストリームから解析

バイト列から(一時ファイル不要)

サーバーレス環境やストリーミングパイプラインで役立ちます。

Rust

use std::io::Cursor;
use office_oxide::{Document, DocumentFormat};

let data = std::fs::read("report.docx")?;
let doc = Document::from_reader(Cursor::new(data), DocumentFormat::Docx)?;
let text = doc.plain_text();

Python

import requests
from office_oxide import Document

data = requests.get("https://example.com/report.docx").content
with Document.from_bytes(data, "docx") as doc:
    print(doc.plain_text())

JavaScript

import { Document } from 'office-oxide';

const res = await fetch('https://example.com/report.docx');
const data = new Uint8Array(await res.arrayBuffer());
using doc = Document.fromBytes(data, 'docx');
console.log(doc.plainText());

C

/* format must be one of: "docx" "xlsx" "pptx" "doc" "xls" "ppt" (lowercase) */
int err = 0;
OfficeDocumentHandle *doc =
    office_document_open_from_bytes(data, len, "docx", &err);
if (doc) {
    char *text = office_document_plain_text(doc, &err);
    if (text) { printf("%s\n", text); office_oxide_free_string(text); }
    office_document_free(doc);
}

WASM

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

const res = await fetch('https://example.com/report.docx');
const data = new Uint8Array(await res.arrayBuffer());
using doc = new WasmDocument(data, 'docx');
console.log(doc.plainText());

パフォーマンス

フォーマット 平均 p99 成功率
DOCX(2,538 ファイル) 0.8ms 3.9ms 98.9%
XLSX(1,802 ファイル) 5.0ms 40ms 97.8%
PPTX(806 ファイル) 0.7ms 3.9ms 98.4%
DOC(246 ファイル) 0.3ms 3.4ms 94.7%
XLS(494 ファイル) 2.8ms 75ms 99.2%
PPT(176 ファイル) 0.7ms 6.6ms 100%

ベンチマーク手法の詳細はパフォーマンスをご覧ください。

関連ドキュメント