Skip to content

提取文档元数据

每种 Office 格式都内嵌一组小小的「核心属性」 — 作者、最后修改日期、标题、主题、关键词 — 以及一组按格式扩展的属性。Office Oxide 把两者都暴露出来。

格式与基本信息

Document 句柄会给出格式名与计数(工作表、幻灯片、section):

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。请用按格式访问的 API:

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 清空它们。

相关链接