Document

Document — Aspose.Note FOSS for Python API Reference

类:Document

Properties: aspose.note Properties: from aspose.note import Document Properties: CompositeNode

Document 是 OneNote 文档对象模型的根节点。它表示单个 OneNote 节点文件 (.one). 所有页面都是直接子节点,属于 Document.


Properties

Document(source=None, load_options=None)
PropertiesPropertiesPropertiesProperties
source`strPathBinaryIO
load_options`LoadOptionsNone`Properties

Properties

from aspose.note import Document

##Load from a file path
doc = Document("MyNotes.one")

##Load from a binary stream
with open("MyNotes.one", "rb") as f:
    doc = Document(f)

##Empty document (no source file)
doc = Document()

构造函数可能抛出的异常

PropertiesProperties
FileCorruptedException无法解析文件
IncorrectDocumentStructureException文件结构不受支持
IncorrectPasswordException文件已加密(不支持加密)
UnsupportedFileFormatException无法识别文件格式

Properties

PropertiesPropertiesPropertiesProperties
DisplayName`strNone`Properties
CreationTime`datetimeNone`Properties
FileFormatFileFormatProperties尽力提供的 OneNote 文件格式版本指示

继承自 CompositeNode

PropertiesPropertiesProperties
FirstChild`NodeNone`
LastChild`NodeNone`

继承自 Node

PropertiesPropertiesProperties
ParentNode`NodeNone`
Document`DocumentNone`

Properties

Count()

len(list(doc)) -> int

返回直接子节点的数量 Page 节点(即该章节中的页面数量)。.

from aspose.note import Document

doc = Document("MyNotes.one")
print(f"Pages: {len(list(doc))}")

Save(target, format_or_options)

doc.Save(target: str | Path | BinaryIO, format_or_options: SaveFormat | SaveOptions | None = None) -> None

将文档保存到文件路径,, pathlib.Path,,或二进制流。目前仅 SaveFormat.Pdf 目前已实现。.

PropertiesPropertiesProperties
target`strPath
format_or_options`SaveFormatSaveOptions

Properties:

  • UnsupportedSaveFormatException: 对任何 SaveFormat 除了 Pdf

关于 PdfSaveOptions.PageIndex / PageCount: 这些字段存在于 dataclass 中,但 未转发至 v26.3.1 版的 PDF 导出器 且无任何影响。完整文档始终会被导出。.

import io
from aspose.note import Document, SaveFormat, PdfSaveOptions

doc = Document("MyNotes.one")

##Save to file path (string)
doc.Save("output.pdf", SaveFormat.Pdf)

##Save to a pathlib.Path
from pathlib import Path
doc.Save(Path("output.pdf"), SaveFormat.Pdf)

##Save to an in-memory stream
buf = io.BytesIO()
doc.Save(buf, PdfSaveOptions(SaveFormat.Pdf))
pdf_bytes = buf.getvalue()

Accept(visitor)

doc.Accept(visitor: DocumentVisitor) -> None

调度访问者遍历整个文档树。调用 visitor.VisitDocumentStart(self),,然后递归访问所有子节点。.

from aspose.note import Document, DocumentVisitor

class MyVisitor(DocumentVisitor):
    def VisitDocumentStart(self, doc):
        print(f"Document: {doc.DisplayName}")

doc = Document("MyNotes.one")
doc.Accept(MyVisitor())

GetPageHistory(page)

doc.GetPageHistory(page: Page) -> list[Page]

返回页面的版本历史。在 v26.3.1 中,这是一段兼容性存根,返回 [page].


DetectLayoutChanges()

doc.DetectLayoutChanges() -> None

兼容性存根。无操作。.


Properties

for page in doc:
    ...

遍历直接子项 Page 文档顺序中的节点。.


完整示例

from aspose.note import Document, Page, RichText, SaveFormat

doc = Document("MyNotes.one")

print(f"Section: {doc.DisplayName}")
print(f"Format:  {doc.FileFormat}")
print(f"Created: {doc.CreationTime}")
print(f"Pages:   {len(list(doc))}")

for page in doc:
    title = page.Title.TitleText.Text if page.Title and page.Title.TitleText else "(untitled)"
    texts = page.GetChildNodes(RichText)
    print(f"  [{title}]  {len(texts)} text node(s)")

doc.Save("output.pdf", SaveFormat.Pdf)
print("Saved output.pdf")

另见

 中文