Com inspeccionar les etiquetes de OneNote a Python
OneNote permet als usuaris anotar contingut amb etiquetes de colors: estrelles, caselles de verificació, senyals importants i etiquetes personalitzades. Aspose.Note FOSS per a Python exposa aquestes anotacions com a NoteTag objectes a RichText, Image, AttachedFile, i Table nodes a través del seu .Tags propietat. Aquesta guia mostra com llegir-los.
Guia pas a pas
Pas 1: Instal·la Aspose.Note FOSS per a Python
pip install aspose-notePas 2: Carrega el fitxer .one
from aspose.note import Document
doc = Document("TaggedNotes.one")
print(f"Pages: {len(list(doc))}")Pas 3: Trobar etiquetes en nodes RichText
La majoria d’etiquetes s’adjunten a blocs de text:
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}")Pas 4: Trobar etiquetes en imatges
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}")Pas 5: Trobar etiquetes en taules
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]}")Pas 6: Recopilar totes les etiquetes de tot el document
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)Referència de la propietat NoteTag
| Propietat | Tipus | Descripció |
|---|---|---|
Icon | `int | None` |
Label | `str | None` |
FontColor | `int | None` |
Highlight | `int | None` |
CreationTime | `datetime | None` |
CompletedTime | `datetime | None` |
Status | TagStatus | TagStatus.Completed si s’ha completat, altrament TagStatus.Open |
Filtrar etiquetes completades vs pendents
Les etiquetes que s’han marcat (com les caselles de verificació “To Do”) tenen un no-None CompletedTime camp:
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}")Crear un NoteTag (en memòria)
El mètode de fàbrica NoteTag.CreateYellowStar() crea un node d’etiqueta que pots adjuntar a nou contingut en memòria:
from aspose.note import NoteTag
tag = NoteTag.CreateYellowStar()
print(f"Created tag: Icon={tag.Icon} Label={tag.Label!r}")La creació en memòria és útil per a la compatibilitat de l’API. Atès que escriure de nou a .one no és compatible, les etiquetes creades no es poden persistir al fitxer.
Problemes comuns
No s’han trobat etiquetes (el document retorna llistes d’etiquetes buides): No totes .one els fitxers contenen etiquetes. Verifiqueu que el document d’origen tingui etiquetes visibles a Microsoft OneNote abans de depurar el codi.
tag.Label és una cadena buida: Algunes formes d’etiqueta no tenen una etiqueta de text a les metadades del fitxer. Utilitzeu tag.Icon per identificar el tipus d’etiqueta de manera programàtica.
Recursos relacionats: