如何在 Python 中检查 OneNote 标签

如何在 Python 中检查 OneNote 标签

OneNote 允许用户使用彩色标签进行注释:星形、复选框、重要标记和自定义标签。Aspose.Note FOSS for Python 将这些注释公开为 NoteTag 对象,位于 RichTextImageAttachedFileTable 节点,通过它们的 .Tags 属性。本指南展示如何读取它们。


分步指南

步骤 1:为 Python 安装 Aspose.Note FOSS

pip install aspose-note

步骤 2:加载 .one 文件

from aspose.note import Document

doc = Document("TaggedNotes.one")
print(f"Pages: {len(list(doc))}")

步骤 3:在 RichText 节点上查找标签

大多数标签附加在文本块上:

from aspose.note import Document, RichText

doc = Document("TaggedNotes.one")
for rt in doc.GetChildNodes(RichText):
    for tag in rt.Tags:
        print(f"[RichText] Label={tag.Label!r}  Icon={tag.Icon}  text={rt.Text.strip()!r}")

步骤 4:在图像上查找标签

from aspose.note import Document, Image

doc = Document("TaggedNotes.one")
for img in doc.GetChildNodes(Image):
    for tag in img.Tags:
        print(f"[Image] Label={tag.Label!r}  filename={img.FileName!r}")

步骤 5:在表格上查找标签

from aspose.note import Document, Table

doc = Document("TaggedNotes.one")
for table in doc.GetChildNodes(Table):
    for tag in table.Tags:
        print(f"[Table] Label={tag.Label!r}  widths={[col.Width for col in table.Columns]}")

第6步:收集文档中的所有标签

from aspose.note import Document, RichText, Image, Table

doc = Document("TaggedNotes.one")
all_tags = []

for rt in doc.GetChildNodes(RichText):
    for tag in rt.Tags:
        all_tags.append({"type": "RichText", "Label": tag.Label,
                         "CompletedTime": tag.CompletedTime, "text": rt.Text.strip()})
for img in doc.GetChildNodes(Image):
    for tag in img.Tags:
        all_tags.append({"type": "Image", "Label": tag.Label,
                         "CompletedTime": tag.CompletedTime, "file": img.FileName})
for table in doc.GetChildNodes(Table):
    for tag in table.Tags:
        all_tags.append({"type": "Table", "Label": tag.Label,
                         "CompletedTime": tag.CompletedTime})

print(f"Total tagged items: {len(all_tags)}")
for item in all_tags:
    print(item)

NoteTag 属性参考

PropertyTypeDescription
Iconint | None数值标签形状标识符(星形、复选框、箭头等)
Labelstr | None可读的标签字符串(例如 "Important""To Do"
FontColorint | None文本颜色的 ARGB 整数
Highlightint | None高亮颜色的 ARGB 整数
CreationTimedatetime | None标签创建时间
CompletedTimedatetime | None标签完成时间(如果未完成则为 None
StatusTagStatus如果已完成则为 TagStatus.Completed,否则为 TagStatus.Open

过滤已完成与待处理标签

已勾选的标签(如 “To Do” 复选框)具有非None CompletedTime 字段:

from aspose.note import Document, RichText

doc = Document("TaggedNotes.one")
pending, done = [], []

for rt in doc.GetChildNodes(RichText):
    for tag in rt.Tags:
        item = {"Label": tag.Label, "text": rt.Text.strip()}
        if tag.CompletedTime is None:
            pending.append(item)
        else:
            done.append(item)

print(f"Pending: {len(pending)}  Done: {len(done)}")
for p in pending:
    print(f"  [ ] {p['Label']}: {p['text']!r}")
for d in done:
    print(f"  [x] {d['Label']}: {d['text']!r}")

创建一个 NoteTag(内存中)

工厂方法 NoteTag.CreateYellowStar() 创建一个标签节点,您可以将其附加到内存中的新内容:

from aspose.note import NoteTag

tag = NoteTag.CreateYellowStar()
print(f"Created tag: Icon={tag.Icon}  Label={tag.Label!r}")

内存创建对于 API 兼容性很有用。由于不支持写回 .one,创建的标签无法持久化到文件。


常见问题

未找到标签(文档返回空标签列表): 并非所有 .one 文件都包含标签。在排查代码之前,请确认源文档在 Microsoft OneNote 中可见标签。

tag.Label 是空字符串:某些标签形状在文件元数据中没有文本标签。使用 tag.Icon 以编程方式识别标签类型。


相关资源:

 中文