Document — Aspose.Note FOSS for Python API Reference

Class: Document

Package: aspose.note Import: from aspose.note import Document Inherits: CompositeNode

Document is the root node of the OneNote document object model. It represents a single OneNote section file (.one). All pages are direct children of Document.


Constructor

Document(source=None, load_options=None)
ParameterTypeRequiredDescription
sourcestr | Path | BinaryIO | NoneNoFile path string, pathlib.Path, binary stream (file handle, io.BytesIO, etc.), or None for an empty document
load_optionsLoadOptions | NoneNoOptional load-time configuration

Examples

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()

Exceptions raised by constructor

ExceptionCondition
FileCorruptedExceptionThe file cannot be parsed
IncorrectDocumentStructureExceptionThe file structure is unsupported
IncorrectPasswordExceptionThe file is encrypted (encryption not supported)
UnsupportedFileFormatExceptionThe file format is not recognized

Properties

PropertyTypeAccessDescription
DisplayNamestr | NoneReadThe display name of the section as stored in the file metadata
CreationTimedatetime | NoneReadSection creation timestamp
FileFormatFileFormatReadAlways returns FileFormat.OneNote2010: hardcoded constant in v26.2, not detected from the file

Inherited from CompositeNode

PropertyTypeDescription
FirstChildNode | NoneFirst direct child (first page)
LastChildNode | NoneLast direct child (last page)

Inherited from Node

PropertyTypeDescription
ParentNodeNode | NoneAlways None for Document (it is the root)
DocumentDocument | NoneReturns self (Document is its own root; never None for a live Document instance)

Methods

Count()

doc.Count() -> int

Returns the number of direct child Page nodes (i.e., the number of pages in the section).

from aspose.note import Document

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

Save(target, format_or_options)

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

Saves the document to a file path, pathlib.Path, or a binary stream. Only SaveFormat.Pdf is currently implemented.

ParameterTypeDescription
targetstr | Path | BinaryIOOutput file path or writable binary stream
format_or_optionsSaveFormat | SaveOptions | NoneTarget format or options object

Exceptions:

  • UnsupportedSaveFormatException: raised for any SaveFormat other than Pdf

Note on PdfSaveOptions.PageIndex / PageCount: These fields exist on the dataclass but are not forwarded to the PDF exporter in v26.2 and have no effect. The full document is always exported.

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

Dispatches the visitor to traverse the entire document tree. Calls visitor.VisitDocumentStart(self), then recursively visits all child nodes.

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]

Returns the version history of a page. In v26.2, this is a compatibility stub that returns [page].


DetectLayoutChanges()

doc.DetectLayoutChanges() -> None

Compatibility stub. No operation.


Iteration

for page in doc:
    ...

Iterates the direct child Page nodes in document order.


Complete Example

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:   {doc.Count()}")

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")

See Also