CompositeNode — Aspose.Note FOSS for Python API Reference
类:CompositeNode
Properties: aspose.note Properties: from aspose.note import CompositeNode Properties: Node
CompositeNode 扩展 Node 具备拥有子节点的能力。文档模型中的所有结构节点类型(Document, Page, Outline, OutlineElement, RichText, Image, Table, TableRow, TableCell, AttachedFile)继承自 CompositeNode.。你永远不要直接实例化 CompositeNode 直接。.
Properties
继承自 Node
| Properties | Properties | Properties | Properties |
|---|---|---|---|
ParentNode | `Node | None` | Properties |
Document | `Document | None` | Properties |
子节点访问属性
| Properties | Properties | Properties | Properties |
|---|---|---|---|
FirstChild | `Node | None` | Properties |
LastChild | `Node | None` | Properties |
Properties
AppendChildLast(node)
composite.AppendChildLast(node: Node) -> NodeProperties node 作为最后一个直接子节点。设置 node.ParentNode = self.。返回 node.
AppendChildFirst(node)
composite.AppendChildFirst(node: Node) -> NodeProperties node 作为第一个直接子节点。返回 node.
InsertChild(index, node)
composite.InsertChild(index: int, node: Node) -> NodeProperties node 在位置 index 在子列表中。返回 node.
RemoveChild(node)
composite.RemoveChild(node: Node) -> NoneProperties node 从子列表和集合 node.ParentNode = None.
GetChildNodes(node_type)
composite.GetChildNodes(node_type: type) -> list递归搜索整个子树(深度优先,包括 self) 并返回所有匹配的节点 node_type. 这是在任何子树中收集给定类的所有实例的主要方式。.
Properties
for child in composite:
...遍历 a CompositeNode 产生它的 仅直接子节点. 使用 GetChildNodes() 进行深度搜索。.
使用示例
遍历直接子节点 vs 深度搜索
from aspose.note import Document, Page, Outline, RichText
doc = Document("MyNotes.one")
first_page = doc.GetChildNodes(Page)[0]
# Direct children of a page (Outline nodes)
print("Direct children of first page:")
for child in first_page:
print(f" {child.NodeType}")
# Deep search — all RichText nodes anywhere under first_page
all_rt = first_page.GetChildNodes(RichText)
print(f"\nAll RichText nodes in first page: {len(all_rt)}")以编程方式构建文档
from aspose.note import Document, Page, Outline, OutlineElement, RichText, SaveFormat, PdfSaveOptions
doc = Document()
page = Page()
outline = Outline()
oe = OutlineElement()
rt = RichText()
rt.Append("Hello, OneNote!")
oe.AppendChildLast(rt)
outline.AppendChildLast(oe)
page.AppendChildLast(outline)
doc.AppendChildLast(page)
doc.Save("output.pdf", PdfSaveOptions(SaveFormat.Pdf))None 安全
FirstChild 和 LastChild 是 None 当节点没有子节点时。始终在解引用之前进行检查。.
另见
- Properties: 所有文档节点的基类
- Properties: 根
CompositeNode - Properties: 页面级
CompositeNode - RichText: 文本块
CompositeNode - 开发者指南