كيفية عبور OneNote Document DOM في Python
Aspose.Note FOSS for Python represents a OneNote section file as a tree of typed Python objects. Understanding how to traverse this tree efficiently is the foundation for all content extraction tasks. This guide covers all three traversal approaches: GetChildNodes,- إيتراation مباشرة، و DocumentVisitor.
نموذج الوثيقة الموضوعية
OneNote DOM هو شجرة صارمة:
Document
├── Page
│ ├── Title
│ │ ├── TitleText (RichText)
│ │ ├── TitleDate (RichText)
│ │ └── TitleTime (RichText)
│ └── Outline
│ └── OutlineElement
│ ├── RichText
│ ├── Image
│ ├── AttachedFile
│ └── Table
│ └── TableRow
│ └── TableCell
│ └── RichText / Image
└── Page (next page ...)كل نود يرث من Node.- الأقواس التي يتمتع بها الأطفال من CompositeNode.
الطريقة 1: GetChildNodes (مكررة، تصفية النوع)
CompositeNode.GetChildNodes(Type) يقوم بإجراء البحث المتكرر في أعماق أولاً عن الشجرة السطحية بأكملها ويعيد قائمة مسطحة بجميع العقد التي تتوافق مع النوع المحدد.:
from aspose.note import Document, RichText, Image, Table, AttachedFile
doc = Document("MyNotes.one")
##All RichText nodes anywhere in the document
texts = doc.GetChildNodes(RichText)
print(f"RichText nodes: {len(texts)}")
##All images
images = doc.GetChildNodes(Image)
print(f"Image nodes: {len(images)}")
##All tables
tables = doc.GetChildNodes(Table)
print(f"Table nodes: {len(tables)}")
##All attachments
attachments = doc.GetChildNodes(AttachedFile)
print(f"AttachedFile nodes: {len(attachments)}")توسيع نطاق البحث إلى صفحة واحدة عن طريق الاتصال GetChildNodes على Page بدلاً من Document:
from aspose.note import Document, Page, RichText
doc = Document("MyNotes.one")
for page in doc.GetChildNodes(Page):
page_texts = page.GetChildNodes(RichText)
print(f" Page has {len(page_texts)} text nodes")الطريقة الثانية: إيثار الطفل المباشر
for child in node إيتيراتس فورًا الأطفال من أ CompositeNode.استخدم هذا عندما تحتاج إلى مستوى معين من الهرم:
from aspose.note import Document
doc = Document("MyNotes.one")
##Direct children of Document are Pages
for page in doc:
title = (
page.Title.TitleText.Text
if page.Title and page.Title.TitleText
else "(untitled)"
)
print(f"Page: {title}")
# Direct children of Page are Outlines (and optionally Title)
for child in page:
print(f" {type(child).__name__}")الطريقة الثالثة: مستندات
DocumentVisitor يقدم نموذج زائر للطريق المنظّم.تجاوز فقط VisitXxxStart/End الطرق التي تحتاجها.يتم إرسال الزائر عن طريق الاتصال doc.Accept(visitor):
from aspose.note import (
Document, DocumentVisitor, Page, Title,
Outline, OutlineElement, RichText, Image,
)
class StructurePrinter(DocumentVisitor):
def __init__(self):
self._depth = 0
def _indent(self):
return " " * self._depth
def VisitPageStart(self, page: Page) -> None:
t = page.Title.TitleText.Text if page.Title and page.Title.TitleText else "(untitled)"
print(f"{self._indent()}Page: {t!r}")
self._depth += 1
def VisitPageEnd(self, page: Page) -> None:
self._depth -= 1
def VisitOutlineStart(self, outline) -> None:
self._depth += 1
def VisitOutlineEnd(self, outline) -> None:
self._depth -= 1
def VisitRichTextStart(self, rt: RichText) -> None:
if rt.Text.strip():
print(f"{self._indent()}Text: {rt.Text.strip()!r}")
def VisitImageStart(self, img: Image) -> None:
print(f"{self._indent()}Image: {img.FileName!r} ({img.Width}x{img.Height}pts)")
doc = Document("MyNotes.one")
doc.Accept(StructurePrinter())أساليب زوار متوفرة
| طريقة زوجين | النوع النود |
|---|---|
VisitDocumentStart/End | Document |
VisitPageStart/End | Page |
VisitTitleStart/End | Title |
VisitOutlineStart/End | Outline |
VisitOutlineElementStart/End | OutlineElement |
VisitRichTextStart/End | RichText |
VisitImageStart/End | Image |
الملاحة على الشجرة
كل عينات عرض ParentNode و أ Document الممتلكات التي يمكن الوصول إليها:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
parent = rt.ParentNode # OutlineElement, TableCell, Title, etc.
root = rt.Document # always the Document root
print(f" '{rt.Text.strip()!r}' parent={type(parent).__name__}")
breakطرق إدارة الأطفال
CompositeNode كما يعرض إدارة الأطفال في ذاكرة (مفيدة لبناء الوثائق البرمجية، على الرغم من أن الكتابة تظهر .one لا يتم دعمها : ):
| طريقة | وصف |
|---|---|
node.FirstChild | أول طفل أو None |
node.LastChild | الطفل الأخير أو None |
node.AppendChildLast(child) | إعطاء الطفل في النهاية |
node.AppendChildFirst(child) | إعطاء الطفل في البداية |
node.InsertChild(index, child) | إدخال في موقف |
node.RemoveChild(child) | إزالة طفل |
حساب النود مع زائر
from aspose.note import Document, DocumentVisitor, Page, RichText, Image
class Counter(DocumentVisitor):
def __init__(self):
self.pages = self.texts = self.images = 0
def VisitPageStart(self, page: Page) -> None:
self.pages += 1
def VisitRichTextStart(self, rt: RichText) -> None:
self.texts += 1
def VisitImageStart(self, img: Image) -> None:
self.images += 1
doc = Document("MyNotes.one")
c = Counter()
doc.Accept(c)
print(f"Pages={c.pages} RichText={c.texts} Images={c.images}")اختيار الطريقة الصحيحة للرحلات
| سيناريو | أفضل النهج |
|---|---|
| العثور على جميع عقدة من نوع واحد (على سبيل المثال، كل RichText) | GetChildNodes(RichText) |
| إيترايد الأطفال المباشرين (لا يتكرر) | for child in node |
| المشي على الشجرة مع السياق (عمق، حالة الأم) | DocumentVisitor |
| الملاحة من المحتوى إلى الوالد أو الجذر | node.ParentNode / node.Document |
الموارد ذات الصلة :