Document

Document — Aspose.Note FOSS for Python API Reference

Lớp: Document

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

Document là nút gốc của mô hình đối tượng tài liệu OneNote. Nó đại diện cho một tệp phần OneNote duy nhất (.one). Tất cả các trang là con trực tiếp của Document.


Enumerations

Document(source=None, load_options=None)
EnumerationsEnumerationsEnumerationsEnumerations
source`strPathBinaryIO
load_options`LoadOptionsNone`Enumerations

Enumerations

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

Các ngoại lệ được ném bởi hàm khởi tạo

EnumerationsEnumerations
FileCorruptedExceptionKhông thể phân tích tệp
IncorrectDocumentStructureExceptionCấu trúc tệp không được hỗ trợ
IncorrectPasswordExceptionTệp tin đã được mã hoá (không hỗ trợ mã hoá)
UnsupportedFileFormatExceptionĐịnh dạng tệp không được nhận dạng

Enumerations

EnumerationsEnumerationsEnumerationsEnumerations
DisplayName`strNone`Enumerations
CreationTime`datetimeNone`Enumerations
FileFormatFileFormatEnumerationsChỉ báo nỗ lực tốt nhất về phiên bản định dạng tệp OneNote

Kế thừa từ CompositeNode

EnumerationsEnumerationsEnumerations
FirstChild`NodeNone`
LastChild`NodeNone`

Kế thừa từ Node

EnumerationsEnumerationsEnumerations
ParentNode`NodeNone`
Document`DocumentNone`

Enumerations

Count()

len(list(doc)) -> int

Trả về số lượng nút con trực tiếp Page (tức là số trang trong phần).

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

Lưu tài liệu vào đường dẫn tệp, pathlib.Path, hoặc một luồng nhị phân. Chỉ SaveFormat.Pdf được hiện thực hoá hiện tại.

EnumerationsEnumerationsEnumerations
target`strPath
format_or_options`SaveFormatSaveOptions

Enumerations:

  • UnsupportedSaveFormatException: được ném ra cho bất kỳ SaveFormat ngoại trừ Pdf

Ghi chú về PdfSaveOptions.PageIndex / PageCount: Các trường này tồn tại trong dataclass nhưng là không được chuyển tiếp tới bộ xuất PDF trong v26.3.1 và không có tác dụng. Toàn bộ tài liệu luôn được xuất.

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

Gửi visitor để duyệt toàn bộ cây tài liệu. Gọi visitor.VisitDocumentStart(self), sau đó đệ quy thăm tất cả các nút con.

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]

Trả về lịch sử phiên bản của một trang. Trong v26.3.1, đây là một stub tương thích trả về [page].


DetectLayoutChanges()

doc.DetectLayoutChanges() -> None

Stub tương thích. Không thực hiện gì.


Enumerations

for page in doc:
    ...

Lặp qua nút con trực tiếp Page các nút theo thứ tự tài liệu.


Ví dụ hoàn chỉnh

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

Xem Thêm

 Tiếng Việt