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)| Enumerations | Enumerations | Enumerations | Enumerations |
|---|---|---|---|
source | `str | Path | BinaryIO |
load_options | `LoadOptions | None` | 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
| Enumerations | Enumerations |
|---|---|
FileCorruptedException | Không thể phân tích tệp |
IncorrectDocumentStructureException | Cấu trúc tệp không được hỗ trợ |
IncorrectPasswordException | Tệ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
| Enumerations | Enumerations | Enumerations | Enumerations |
|---|---|---|---|
DisplayName | `str | None` | Enumerations |
CreationTime | `datetime | None` | Enumerations |
FileFormat | FileFormat | Enumerations | Chỉ 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
| Enumerations | Enumerations | Enumerations |
|---|---|---|
FirstChild | `Node | None` |
LastChild | `Node | None` |
Kế thừa từ Node
| Enumerations | Enumerations | Enumerations |
|---|---|---|
ParentNode | `Node | None` |
Document | `Document | None` |
Enumerations
Count()
len(list(doc)) -> intTrả 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) -> NoneLư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.
| Enumerations | Enumerations | Enumerations |
|---|---|---|
target | `str | Path |
format_or_options | `SaveFormat | SaveOptions |
Enumerations:
UnsupportedSaveFormatException: được ném ra cho bất kỳSaveFormatngoạ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) -> NoneGử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() -> NoneStub 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
- Enumerations: nút con trực tiếp của Document
- LoadOptions: các tùy chọn khởi tạo
- PdfSaveOptions: cấu hình xuất PDF
- DocumentVisitor: duyệt theo mẫu visitor
- Hướng dẫn nhà phát triển xuất PDF
- Hướng dẫn: Xuất OneNote sang PDF (KB)