कैसे Python में OneNote दस्तावेज़ DOM को ट्रैवर्स करें
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, प्रत्यक्ष पुनरावृत्ति, और 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")विधि 2: प्रत्यक्ष चाइल्ड इटरेशन
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__}")विधि 3: DocumentVisitor
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 |
संबंधित संसाधन: