提取文档元数据
每种 Office 格式都内嵌了一组「核心属性」——作者、最后修改日期、标题、主题、关键词——以及每种格式独有的扩展属性。Office Oxide 将两者都暴露出来。
格式与基本信息
Document 句柄提供格式名称及各类计数(工作表、幻灯片、节):
Rust
use office_oxide::{Document, DocumentFormat};
let doc = Document::open("report.docx")?;
assert_eq!(doc.format(), DocumentFormat::Docx);
Python
from office_oxide import Document
with Document.open("report.docx") as doc:
print(doc.format) # "docx"
print(doc.detect_format()) # confirms via magic bytes
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"
C
int err = 0;
OfficeDocumentHandle *doc = office_document_open("report.docx", &err);
if (!doc) { fprintf(stderr, "open failed: code=%d\n", err); return 1; }
const char *fmt = office_document_format(doc); /* "docx" — do NOT free */
printf("%s\n", fmt);
office_document_free(doc);
WASM
import { WasmDocument } from 'office-oxide-wasm';
const data = new Uint8Array(await (await fetch('/report.docx')).arrayBuffer());
using doc = new WasmDocument(data, 'docx');
console.log(doc.formatName()); // "docx"
核心属性
对于 OOXML 格式,核心属性存储于 docProps/core.xml。原生绑定提供带类型的 core_properties() 访问器;Go、C#、C 和 WASM 则通过统一 JSON IR 的 metadata 块读取相同字段。
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);
}
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
Go
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());
C
int err = 0;
OfficeDocumentHandle *doc = office_document_open("report.docx", &err);
char *ir_json = office_document_to_ir_json(doc, &err); /* DocumentIR as JSON */
if (ir_json) {
/* parse with your JSON lib; read ir["metadata"]["title"], ["author"], ... */
office_oxide_free_string(ir_json);
}
office_document_free(doc);
WASM
import { WasmDocument } from 'office-oxide-wasm';
const data = new Uint8Array(await (await fetch('/report.docx')).arrayBuffer());
using doc = new WasmDocument(data, 'docx');
const meta = doc.toIr().metadata; // JS object
console.log(meta.title, meta.author, meta.created);
as_xlsx() 和 as_pptx() 上同样存在 core_properties() 访问器。
扩展(应用)属性
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() 提供 slides、notes、presentation_format 等字段。对于 XLSX,则提供工作表名称列表。
自定义属性
core_properties() 和 app_properties() 覆盖标准属性集。若要获取应用程序自定义键(通过 Word 的「文档属性 → 高级属性」设置),请使用:
Python
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() 结构:
Python
with Document.open("legacy.doc") as doc:
props = doc.as_doc().core_properties()
print(props.author, props.created)
为何重要
- 搜索索引 — 标题、作者和关键词驱动文档发现。
- 合规性 — 最后修改日期、创建者和修订历史构成审计追踪的基础。
- 去重 — 作者 + 标题 + 创建日期是低成本却有效的文档指纹。
- 清除 PII — 发布前先读取核心属性,再用
EditableDocument将其清空。