Як перевірити теги OneNote у Python

Як перевірити теги OneNote у Python

OneNote дозволяє користувачам анотувати вміст за допомогою кольорових міток: зірок, прапорців‑чекбоксів, важливих позначок та власних міток. Aspose.Note FOSS for Python надає ці анотації у вигляді NoteTag об’єктів у вузлах RichText, Image, AttachedFile та Table через їхню властивість .Tags. У цьому посібнику показано, як їх читати.


Покроковий посібник

Крок 1: Встановіть Aspose.Note FOSS для Python

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, якщо не завершена)
StatusTagStatusTagStatus.Completed, якщо завершено, інакше TagStatus.Open

Фільтр завершених проти очікуючих тегів

Теги, які були позначені (наприклад, прапорці «To Do») мають поле 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}")

Створити NoteTag (в пам’яті)

Фабричний метод NoteTag.CreateYellowStar() створює вузол тегу, який ви можете прикріпити до нового вмісту в пам’яті:

from aspose.note import NoteTag

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

Створення в пам’яті корисне для сумісності API. Оскільки запис назад у .one не підтримується, створені теги не можуть бути збережені у файл.


Поширені проблеми

No tags found (document returns empty Tags lists): Not all .one files contain tags. Verify the source document has tags visible in Microsoft OneNote before troubleshooting the code.

tag.Label є порожнім рядком: Деякі форми тегів не мають текстової мітки у метаданих файлу. Використовуйте tag.Icon для ідентифікації типу тегу програмно.


Пов’язані ресурси:

 Українська