Skip to content

ドキュメントメタデータの抽出

すべての Office フォーマットには小さな「コアプロパティ」セット — 作成者、最終更新日、タイトル、件名、キーワード — に加え、フォーマット固有の拡張セットが埋め込まれています。Office Oxide は両方を公開します。

フォーマットと基本情報

Document ハンドルはフォーマット名と件数(シート、スライド、セクション)を公開します:

Python

from office_oxide import Document

with Document.open("report.docx") as doc:
    print(doc.format)            # "docx"
    print(doc.detect_format())   # マジックバイトで確認

Rust

use office_oxide::{Document, DocumentFormat};

let doc = Document::open("report.docx")?;
assert_eq!(doc.format(), DocumentFormat::Docx);

JavaScript

using doc = Document.open('report.docx');
console.log(doc.format);        // "docx"

Go

doc, err := officeoxide.Open("report.docx")
if err != nil { log.Fatal(err) }
defer doc.Close()

fmtName, _ := doc.Format()   // "docx"
fmt.Println(fmtName)

C#

using OfficeOxide;

using var doc = Document.Open("report.docx");
Console.WriteLine(doc.Format);   // "docx"

コアプロパティ

OOXML フォーマットでは、コアプロパティは docProps/core.xml にあります。フォーマット固有のアクセサを使用:

Python

from office_oxide import Document

with Document.open("report.docx") as doc:
    docx = doc.as_docx()
    props = docx.core_properties()
    print(props.title)            # str | None
    print(props.author)           # str | None
    print(props.created)          # datetime | None
    print(props.modified)         # datetime | None
    print(props.subject)          # str | None
    print(props.keywords)         # str | None

Rust

use office_oxide::Document;

let doc = Document::open("report.docx")?;
if let Some(docx) = doc.as_docx() {
    let props = docx.core_properties();
    println!("{:?}", props.title);
    println!("{:?}", props.author);
}

as_xlsx()as_pptx() にも同じ core_properties() アクセサがあります。

Go

Go は統一 JSON IR 経由でコアプロパティを公開します:

doc, _ := officeoxide.Open("report.docx")
defer doc.Close()

irJSON, _ := doc.ToIRJSON()
var ir struct {
    Metadata struct {
        Title    string `json:"title"`
        Author   string `json:"author"`
        Created  string `json:"created"`
        Modified string `json:"modified"`
        Subject  string `json:"subject"`
        Keywords string `json:"keywords"`
    } `json:"metadata"`
}
json.Unmarshal([]byte(irJSON), &ir)
fmt.Println(ir.Metadata.Title, ir.Metadata.Author)

C#

using OfficeOxide;
using System.Text.Json;

using var doc = Document.Open("report.docx");
using var ir = JsonDocument.Parse(doc.ToIrJson());

var meta = ir.RootElement.GetProperty("metadata");
Console.WriteLine(meta.GetProperty("title").GetString());
Console.WriteLine(meta.GetProperty("author").GetString());
Console.WriteLine(meta.GetProperty("created").GetString());

拡張(app)プロパティ

docProps/app.xml には拡張メタデータが含まれます: ページ数、単語数、段落数、スライド数、アプリケーション名とバージョン、ハイパーリンクなど。

Python

with Document.open("report.docx") as doc:
    app = doc.as_docx().app_properties()
    print(app.application)   # "Microsoft Office Word"
    print(app.app_version)
    print(app.pages)
    print(app.words)
    print(app.paragraphs)

PPTX では app_properties()slidesnotespresentation_format などを公開。XLSX ではシート名リストを公開します。

カスタムプロパティ

core_properties()app_properties() は標準セットをカバーします。アプリケーション定義のカスタムキー(Word の「ドキュメントプロパティ → 詳細プロパティ」で設定)には:

with Document.open("report.docx") as doc:
    custom = doc.as_docx().custom_properties()
    for name, value in custom.items():
        print(name, value)

レガシーフォーマット

DOC、XLS、PPT は OLE2 の \005SummaryInformation および \005DocumentSummaryInformation ストリームにメタデータを格納します。Office Oxide はこれらを同じ core_properties() 形にパース:

with Document.open("legacy.doc") as doc:
    props = doc.as_doc().core_properties()
    print(props.author, props.created)

なぜこれが重要か

  • 検索インデックス化 — タイトル、作成者、キーワードがドキュメント発見を助けます。
  • コンプライアンス — 最終更新日、作成者、リビジョン履歴が監査トレイルになります。
  • 重複排除 — 作成者 + タイトル + 作成日は安価で効果的なフィンガープリント。
  • PII の削除 — 公開前にコアプロパティを読み、EditableDocument でクリアします。

関連項目