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

คืนค่าจำนวน child โดยตรง 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 แต่ ไม่ได้ส่งต่อไปยังตัวส่งออก PDF ในเวอร์ชัน v26.3.1 และไม่มีผลกระทบ. เอกสารเต็มจะถูกส่งออกเสมอ.

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 เพื่อสำรวจต้นไม้เอกสารทั้งหมด. เรียก 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")

ดูเพิ่มเติม

 ภาษาไทย