คู่มือผู้พัฒนา

Aspose.Note FOSS for Python is a free, open-source library for reading Microsoft OneNote .one ไฟล์ส่วนที่ไม่มีการพึ่งพา Microsoft Office แต่อย่างใด มันให้ API สาธารณะที่สะอาดภายใต้ the aspose.note แพคเกจ, ออกแบบตาม Aspose.Note สำหรับอินเทอร์เฟซ .NET. ไลบรารีนี้เหมาะสำหรับการทำอัตโนมัติเอกสาร, การทำดัชนีเนื้อหา, สายงานการสกัดข้อมูล, และกระบวนการทำงานด้านการจัดเก็บเอกสาร.

คู่มือสำหรับนักพัฒนานี้ครอบคลุมพื้นที่ API สาธารณะทั้งหมดที่มีในเวอร์ชัน 26.3.1 พร้อมตัวอย่างโค้ดที่สามารถรันได้สำหรับทุกฟีเจอร์หลัก.

การโหลดเอกสาร

โหลด .one ไฟล์จากเส้นทางไฟล์หรือสตรีมไบนารี. The Document คลาสเป็นจุดเริ่มต้นสำหรับการดำเนินการทั้งหมด.

โหลดจากเส้นทางไฟล์

from aspose.note import Document

doc = Document("MyNotes.one")

โหลดจากสตรีมไบนารี

มีประโยชน์เมื่ออ่านจากที่เก็บข้อมูลบนคลาวด์, การตอบสนอง HTTP, หรือบัฟเฟอร์ในหน่วยความจำ:

from pathlib import Path
from aspose.note import Document

with Path("MyNotes.one").open("rb") as f:
    doc = Document(f)

ตัวเลือกการโหลด

ใช้ LoadOptions เพื่อกำหนดพารามิเตอร์ทางเลือกในขณะโหลด:

from aspose.note import Document, LoadOptions

opts = LoadOptions()
opts.LoadHistory = True   # Include page history in the DOM
doc = Document("MyNotes.one", opts)

หมายเหตุ: DocumentPassword มีอยู่บน LoadOptions เพื่อความเข้ากันได้ของ API, แต่เอกสารที่เข้ารหัสไม่ได้รับการสนับสนุน การพยายามโหลดไฟล์ที่เข้ารหัสจะทำให้เกิด IncorrectPasswordException.


โครงสร้างเอกสาร (DOM)

โมเดลเอกสาร OneNote เป็นโครงสร้างต้นไม้:

Document
  └── Page (0..n)
        ├── Title
        │     ├── TitleText (RichText)
        │     ├── TitleDate (RichText)
        │     └── TitleTime (RichText)
        └── Outline (0..n)
              └── OutlineElement (0..n)
                    ├── RichText
                    ├── Image
                    ├── Table
                    │     └── TableRow
                    │           └── TableCell
                    │                 └── RichText / Image
                    └── AttachedFile

แต่ละโหนดแสดง ParentNode และ Document คุณสมบัติที่เดินขึ้นไปยังราก. โหนดเชิงประกอบสนับสนุนการวนซ้ำของลูก, FirstChild, LastChild, AppendChildLast, InsertChild, RemoveChild, และ GetChildNodes(Type).


การวนซ้ำหน้า

หน้าเป็นลูกโดยตรงของ Document. วนซ้ำพวกมันโดยตรงหรือใช้ GetChildNodes:

from aspose.note import Document, Page

doc = Document("MyNotes.one")

for page in doc:
    title = page.Title.TitleText.Text if page.Title and page.Title.TitleText else "(untitled)"
    author = page.Author or "(unknown)"
    print(f"  {title}  [by {author}]")

เมตาดาต้าของหน้า:

คุณสมบัติประเภทคำอธิบาย
Title`TitleNone`
Author`strNone`
CreationTime`datetimeNone`
LastModifiedTime`datetimeNone`
Level`intNone`

การสกัดข้อความ

สกัดข้อความธรรมดาทั้งหมด

from aspose.note import Document, RichText

doc = Document("MyNotes.one")
all_text = [rt.Text for rt in doc.GetChildNodes(RichText) if rt.Text]
print("\n".join(all_text))

ตรวจสอบการจัดรูปแบบที่ต่อเนื่อง

แต่ละ RichText มีรายการของ TextRun ส่วน. แต่ละรันมีของตนเอง TextStyle:

from aspose.note import Document, RichText

doc = Document("FormattedNotes.one")
for rt in doc.GetChildNodes(RichText):
    for run in rt.TextRuns:
        style = run.Style
        flags = []
        if style.IsBold: flags.append("bold")
        if style.IsItalic: flags.append("italic")
        if style.IsHyperlink: flags.append(f"link={style.HyperlinkAddress}")
        print(f"{run.Text!r:40s} [{', '.join(flags)}]")

สกัดลิงก์ไฮเปอร์

from aspose.note import Document, RichText

doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
    for run in rt.TextRuns:
        if run.Style.IsHyperlink and run.Style.HyperlinkAddress:
            print(run.Text, "->", run.Style.HyperlinkAddress)

การสกัดภาพ

from aspose.note import Document, Image

doc = Document("MyNotes.one")
for i, img in enumerate(doc.GetChildNodes(Image), start=1):
    name = img.FileName or f"image_{i}.bin"
    with open(name, "wb") as f:
        f.write(img.Bytes)
    print(f"Saved {name}  ({img.Width}x{img.Height})")

คุณสมบัติของภาพ: FileName, Bytes, Width, Height, AlternativeTextTitle, AlternativeTextDescription, HyperlinkUrl, Tags.


การแยกวิเคราะห์ตาราง

from aspose.note import Document, Table, TableRow, TableCell, RichText

doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
    print("Column widths:", [col.Width for col in table.Columns])
    for r, row in enumerate(table.GetChildNodes(TableRow), start=1):
        cells = row.GetChildNodes(TableCell)
        row_text = [
            " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
            for cell in cells
        ]
        print(f"Row {r}:", row_text)

ไฟล์ที่แนบมา

from aspose.note import Document, AttachedFile

doc = Document("NotesWithAttachments.one")
for i, af in enumerate(doc.GetChildNodes(AttachedFile), start=1):
    name = af.FileName or f"attachment_{i}.bin"
    with open(name, "wb") as f:
        f.write(af.Bytes)
    print(f"Saved: {name}")

แท็กและรายการลำดับเลข

ตรวจสอบรายการ NoteTag

from aspose.note import Document, RichText, Image, Table

doc = Document("TaggedNotes.one")
for rt in doc.GetChildNodes(RichText):
    for tag in rt.Tags:
        print(f"RichText tag: {tag.Label} icon={tag.Icon}")
for img in doc.GetChildNodes(Image):
    for tag in img.Tags:
        print(f"Image tag: {tag.Label}")

ตรวจสอบรายการลำดับเลข

from aspose.note import Document, OutlineElement

doc = Document("NumberedNotes.one")
for oe in doc.GetChildNodes(OutlineElement):
    nl = oe.NumberList
    if nl:
        print(f"format={nl.Format!r}")

รูปแบบ DocumentVisitor

ใช้ DocumentVisitor เพื่อ implement visitor ที่เดินทางผ่านต้นไม้ของเอกสารทั้งหมด:

from aspose.note import Document, DocumentVisitor, Page, RichText, Image

class ContentCounter(DocumentVisitor):
    def __init__(self):
        self.pages = 0
        self.texts = 0
        self.images = 0

    def VisitPageStart(self, page: Page) -> None:
        self.pages += 1

    def VisitRichTextStart(self, rt: RichText) -> None:
        self.texts += 1

    def VisitImageStart(self, img: Image) -> None:
        self.images += 1

doc = Document("MyNotes.one")
counter = ContentCounter()
doc.Accept(counter)
print(f"Pages: {counter.pages}, Texts: {counter.texts}, Images: {counter.images}")

การส่งออก PDF

การส่งออก PDF ต้องการไลบรารีเสริม ReportLab. ติดตั้งโดยใช้:

pip install "aspose-note[pdf]"

การส่งออก PDF พื้นฐาน

from aspose.note import Document, SaveFormat

doc = Document("MyNotes.one")
doc.Save("output.pdf", SaveFormat.Pdf)

การส่งออก PDF พร้อมตัวเลือก

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

doc = Document("MyNotes.one")

##With save options
opts = PdfSaveOptions()
doc.Save("output.pdf", opts)

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

หมายเหตุ: PdfSaveOptions.PageIndex และ PageCount ฟิลด์มีอยู่แต่ไม่ได้ส่งต่อไปยังตัวส่งออก PDF ในเวอร์ชัน v26.3.1 เอกสารทั้งหมดจะถูกส่งออกเสมอ.


ข้อจำกัดปัจจุบัน

พื้นที่สถานะ
การอ่าน .one ไฟล์รองรับเต็มที่
การส่งออก PDF (ผ่าน ReportLab)รองรับ
กำลังเขียนกลับไปยัง .oneยังไม่ได้ดำเนินการ
เอกสารที่เข้ารหัสไม่รองรับ (จะทำให้เกิด IncorrectPasswordException)
รูปแบบการบันทึก HTML / ภาพ / ONEประกาศเพื่อความเข้ากันได้ของ API; ทำให้เกิด UnsupportedSaveFormatException

คู่มือที่พร้อมใช้งาน


ดูเพิ่มเติม

 ภาษาไทย