Come ispezionare i tag di OneNote in Python
OneNote consente agli utenti di annotare i contenuti con tag colorati: stelle, caselle di controllo, flag importanti ed etichette personalizzate. Aspose.Note FOSS per Python espone queste annotazioni come oggetti NoteTag sui nodi RichText, Image, AttachedFile e Table tramite la loro proprietà .Tags. Questa guida mostra come leggerle.
Guida passo-passo
Passo 1: Installa Aspose.Note FOSS per Python
pip install aspose-notePasso 2: Carica il file .one
from aspose.note import Document
doc = Document("TaggedNotes.one")
print(f"Pages: {len(list(doc))}")Passo 3: Trova i tag nei nodi RichText
La maggior parte dei tag è associata a blocchi di testo:
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}")Passo 4: Trova tag nelle immagini
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}")Passo 5: Trova tag sulle tabelle
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]}")Passo 6: Raccogli tutti i tag nel documento
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)Riferimento alla proprietà NoteTag
| Property | Type | Description |
|---|---|---|
Icon | int | None | Identificatore numerico della forma del tag (stella, casella di controllo, freccia, ecc.) |
Label | str | None | Stringa di etichetta leggibile dall’uomo (ad es. "Important", "To Do") |
FontColor | int | None | Colore del testo come intero ARGB |
Highlight | int | None | Colore di evidenziazione come intero ARGB |
CreationTime | datetime | None | Quando il tag è stato creato |
CompletedTime | datetime | None | Quando il tag è stato completato (None se non completato) |
Status | TagStatus | TagStatus.Completed se completato, altrimenti TagStatus.Open |
Filtra i tag completati vs in sospeso
I tag che sono stati spuntati (come le caselle di controllo “To Do”) hanno un campo non-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}")Crea un NoteTag (In-Memory)
Il metodo factory NoteTag.CreateYellowStar() crea un nodo tag che puoi allegare al nuovo contenuto in memoria:
from aspose.note import NoteTag
tag = NoteTag.CreateYellowStar()
print(f"Created tag: Icon={tag.Icon} Label={tag.Label!r}")La creazione in memoria è utile per la compatibilità dell’API. Poiché la scrittura su
.onenon è supportata, i tag creati non possono essere persistiti su file.
Problemi comuni
Nessun tag trovato (il documento restituisce elenchi di tag vuoti): Non tutti i file .one contengono tag. Verifica che il documento di origine abbia i tag visibili in Microsoft OneNote prima di risolvere i problemi del codice.
tag.Label è una stringa vuota: Alcune forme di tag non hanno un’etichetta di testo nei metadati del file. Usa tag.Icon per identificare il tipo di tag programmaticamente.
Risorse correlate: