ภาพรวมของคุณลักษณะ — Aspose.Note FOSS สำหรับ Python

Aspose.Note FOSS for Python (package aspose-note, version 26.3.1) ให้ Python API สำหรับการอ่าน Microsoft OneNote .one ไฟล์ส่วนและการส่งออกเป็น PDF คุณลักษณะทั้งหมดที่ระบุด้านล่างได้รับการตรวจสอบกับซอร์สโค้ดของรีโพซิทอรี, README, และสคริปต์ตัวอย่าง.

การติดตั้งและตั้งค่า

ติดตั้งจาก PyPI:

pip install aspose-note

สำหรับการสนับสนุนการส่งออกเป็น PDF (ต้องการ ReportLab):

pip install "aspose-note[pdf]"

ข้อกำหนด: Python 3.10 หรือใหม่กว่า ไม่จำเป็นต้องติดตั้ง Microsoft Office.


คุณสมบัติและความสามารถ

.การโหลดไฟล์ .one

โหลดไฟล์ส่วนของ Microsoft OneNote จากเส้นทางไฟล์หรือสตรีมไบนารีใด ๆ (ไฟล์แฮนด์เดิล, io.BytesIO, เนื้อหาตอบกลับ HTTP, สตรีมการจัดเก็บบนคลาวด์).

  • Document.FileFormat ให้การบ่งชี้เวอร์ชันของรูปแบบไฟล์ OneNote อย่างดีที่สุด (OneNote2010, OneNoteOnline, หรือ OneNote2007)
  • การโหลดแบบสตรีมช่วยขจัดการอ่าน/เขียนดิสก์สำหรับเวิร์กโฟลว์ในหน่วยความจำหรือเครือข่าย
  • LoadOptions.LoadHistory แฟล็กควบคุมว่าประวัติหน้าถูกใส่ใน DOM หรือไม่
from aspose.note import Document

##From a file path
doc = Document("notebook.one")

##From a binary stream
with open("notebook.one", "rb") as f:
    doc = Document(f)

การเดินทางผ่าน Document DOM

เอกสาร OneNote ทั้งหมดถูกเปิดเผยเป็นต้นไม้ของอ็อบเจกต์ Python ที่มีประเภท ทุกโหนดสืบทอดจาก Node หรือ CompositeNode:

  • Document: root; เปิดเผย DisplayName, CreationTime, FileFormat
  • Page: ลูกโดยตรงของ Document; เปิดเผย Title, Author, CreationTime, LastModifiedTime, Level
  • Title: เปิดเผย TitleText, TitleDate, TitleTime (ทั้งหมด RichText โหนด)
  • Outline: ตัวคอนเทนเนอร์เชิงตำแหน่งที่มี HorizontalOffset, VerticalOffset, MaxWidth, MaxHeight, MinWidth, ReservedWidth, IndentPosition
  • OutlineElement: ตัวคอนเทนเนอร์ใบไม้; เปิดเผย NumberList

วิธีการนำทางบน CompositeNode:

เมธอด / คุณสมบัติคำอธิบาย
FirstChild, LastChildการเข้าถึงลูกโดยตรง
GetChildNodes(Type)การค้นหาแบบเรียกซ้ำ, กรองตามประเภท
AppendChildLast(node)เพิ่มลูกที่ตำแหน่งสุดท้าย
AppendChildFirst(node)เพิ่มลูกที่ตำแหน่งเริ่มต้น
InsertChild(index, node)แทรกที่ตำแหน่ง
RemoveChild(node)ลบลูก
for child in nodeวนซ้ำลูกโดยตรง
from aspose.note import Document, Page, Outline, OutlineElement, RichText

doc = Document("notebook.one")
for page in doc.GetChildNodes(Page):
    title_text = page.Title.TitleText.Text if page.Title and page.Title.TitleText else ""
    print(f"Page: {title_text}")
    for outline in page.GetChildNodes(Outline):
        for oe in outline.GetChildNodes(OutlineElement):
            for rt in oe.GetChildNodes(RichText):
                print(f"  {rt.Text}")

การสกัดเนื้อหา Rich Text

RichText โหนดเปิดเผย:

  • Text: str: สตริงข้อความธรรมดาเต็มรูปแบบ
  • TextRuns: list[TextRun]: รายการของส่วนที่จัดรูปแบบ
  • Tags: list[NoteTag]: แท็ก OneNote ที่แนบกับบล็อกนี้
  • Append(text, style=None): เพิ่มรันข้อความในหน่วยความจำ
  • Replace(old_value, new_value): การแทนที่สตริงในหน่วยความจำ

แต่ละ TextRun มี:

คุณสมบัติประเภทคำอธิบาย
Textstrข้อความส่วน
StyleTextStyleเมตาดาต้าการจัดรูปแบบ

TextStyle คุณสมบัติ:

คุณสมบัติประเภท
IsBold, IsItalic, IsUnderline, IsStrikethroughbool
IsSuperscript, IsSubscriptbool
FontName`str
FontSize`float
FontColor, Highlight`int
Language`int
IsHyperlinkbool
HyperlinkAddress`str
from aspose.note import Document, RichText

doc = Document("notebook.one")
for rt in doc.GetChildNodes(RichText):
    for run in rt.TextRuns:
        if run.Style.IsHyperlink:
            print(f"Hyperlink: {run.Text} -> {run.Style.HyperlinkAddress}")
        if run.Style.IsBold:
            print(f"Bold text: {run.Text}")

การสกัดภาพ

Image โหนดเปิดเผยไบต์ดิบและเมตาดาต้าสำหรับแต่ละภาพที่ฝังอยู่:

คุณสมบัติประเภทคำอธิบาย
FileName`strNone`
Bytesbytesข้อมูลภาพดิบ
Width, Height`floatNone`
AlternativeTextTitle`strNone`
AlternativeTextDescription`strNone`
HyperlinkUrl`strNone`
Tagslist[NoteTag]แท็ก OneNote ที่แนบมา
from aspose.note import Document, Image

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

การสกัดไฟล์ที่แนบ

AttachedFile โหนดเปิดเผยไฟล์แนบที่ฝังอยู่:

คุณสมบัติประเภทคำอธิบาย
FileName`strNone`
Bytesbytesข้อมูลไฟล์ดิบ
Tagslist[NoteTag]แท็ก OneNote ที่แนบ
from aspose.note import Document, AttachedFile

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

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

Table, TableRow, และ TableCell เปิดเผยโครงสร้างตารางทั้งหมด:

คลาสคุณสมบัติหลัก
TableColumns: list[TableColumn] (แต่ละ TableColumn มี .Width และ .LockedWidth), IsBordersVisible: bool, Tags: list[NoteTag]
TableRowวนซ้ำเซลล์ผ่าน GetChildNodes(TableCell)
TableCellประกอบด้วย RichText, Image, และโหนดเนื้อหาอื่น
from aspose.note import Document, Table, TableRow, TableCell, RichText

doc = Document("notebook.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)
        values = [
            " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
            for cell in cells
        ]
        print(f"Row {r}:", values)

การตรวจสอบแท็ก OneNote

NoteTag ปรากฏบน RichText, Image, AttachedFile, และ Table โหนดผ่าน .Tags คุณสมบัติ. OutlineElement ไม่มี .Tags คุณสมบัติ. NoteTag คุณสมบัติ:

คุณสมบัติประเภทคำอธิบาย
Icon`intNone`
Label`strNone`
FontColor`intNone`
Highlight`intNone`
CreationTime`datetimeNone`
CompletedTime`datetimeNone`

เมธอดแฟคทอรี: NoteTag.CreateYellowStar() สร้างโหนดแท็กดาวสีเหลืองมาตรฐาน.

from aspose.note import Document, RichText

doc = Document("notebook.one")
for rt in doc.GetChildNodes(RichText):
    for tag in rt.Tags:
        print(f"Tag: {tag.Label} (icon={tag.Icon}, completed={tag.CompletedTime})")

การสนับสนุนรายการลำดับเลข

OutlineElement.NumberList เปิดเผย:

คุณสมบัติประเภทคำอธิบาย
Format`strNone`
Restart`intNone`
from aspose.note import Document, OutlineElement

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

การท่อง DocumentVisitor

DocumentVisitor ให้รูปแบบ Visitor สำหรับการท่องเอกสารเต็มรูปแบบแบบโครงสร้าง. Override any VisitXxxStart / VisitXxxEnd เมธอดเพื่อดักจับประเภทโหนดเฉพาะ:

  • VisitDocumentStart(doc) / VisitDocumentEnd(doc)
  • VisitPageStart(page) / VisitPageEnd(page)
  • VisitTitleStart(title) / VisitTitleEnd(title)
  • VisitOutlineStart(outline) / VisitOutlineEnd(outline)
  • VisitOutlineElementStart(oe) / VisitOutlineElementEnd(oe)
  • VisitRichTextStart(rt) / VisitRichTextEnd(rt)
  • VisitImageStart(img) / VisitImageEnd(img)
from aspose.note import Document, DocumentVisitor, Page, RichText, Image

class MySummaryVisitor(DocumentVisitor):
    def __init__(self):
        self.pages, self.texts, self.images = 0, 0, 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("notebook.one")
v = MySummaryVisitor()
doc.Accept(v)
print(f"Pages={v.pages}  RichText={v.texts}  Images={v.images}")

การส่งออก PDF

บันทึกเอกสารที่โหลดแล้วเป็น PDF โดยใช้ Document.Save(). รองรับผ่านแบ็กเอนด์ ReportLab ทางเลือก.

from aspose.note import Document, SaveFormat

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

PdfSaveOptions

ตัวเลือกประเภทคำอธิบาย
PageIndexintฟิลด์มีอยู่; ไม่ได้ส่งต่อไปยังตัวส่งออก PDF ในเวอร์ชัน 26.3.1 (ไม่มีผล)
PageCount`intNone`
ImageCompression`AnyNone`
JpegQuality`intNone`
PageSettings`AnyNone`
PageSplittingAlgorithm`AnyNone`
import io
from aspose.note import Document, SaveFormat
from aspose.note.saving import PdfSaveOptions

doc = Document("notebook.one")

##Save to file
doc.Save("output.pdf", SaveFormat.Pdf)

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

อ้างอิงรูปแบบและ Enum

SaveFormat

ค่าสถานะ
SaveFormat.Pdfได้ทำการใช้งาน (ต้องการ ReportLab)

FileFormat

Document.FileFormat ให้การบ่งชี้โดยประมาณของเวอร์ชันรูปแบบไฟล์ OneNote. enum ประกาศสามค่า:

ค่าคำอธิบาย
FileFormat.OneNote2010รูปแบบ OneNote 2010
FileFormat.OneNoteOnlineรูปแบบ OneNote Online
FileFormat.OneNote2007รูปแบบ OneNote 2007

NodeType

NodeType.Document, NodeType.Page, NodeType.Outline, NodeType.OutlineElement, NodeType.RichText, NodeType.Image, NodeType.Table, NodeType.AttachedFile

HorizontalAlignment

HorizontalAlignment.Left, HorizontalAlignment.Center, HorizontalAlignment.Right


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

ข้อจำกัดรายละเอียด
อ่านอย่างเดียวเขียนกลับไปยัง .one รูปแบบยังไม่ได้ทำการใช้งาน
ไม่มีการเข้ารหัสเอกสารที่เข้ารหัสทำให้เกิด IncorrectPasswordException
PDF เท่านั้นสำหรับการส่งออกอื่น ๆ SaveFormat ค่าทำให้เกิด UnsupportedSaveFormatException
ต้องใช้ ReportLab สำหรับ PDFติดตั้ง pip install "aspose-note[pdf]" แยกต่างหาก
GetPageHistory คืนค่าเป็นรายการที่มีหนึ่งองค์ประกอบการท่องประวัติเพจเต็มเป็นส่วนจำลอง; คืนค่า [page]
DetectLayoutChanges()ส่วนจำลองความเข้ากันได้; ไม่มีการดำเนินการ

เคล็ดลับและแนวปฏิบัติที่ดีที่สุด

  • ตรวจสอบค่า None: Page.Title, Title.TitleText, OutlineElement.NumberList, และฟิลด์เมตาดาต้าส่วนใหญ่สามารถเป็น None. ควรตรวจสอบเสมอด้วย if x is not None หรือ if x ก่อนเข้าถึงคุณสมบัติ.
  • ใช้ GetChildNodes(Type) สำหรับการค้นหาแบบเรียกซ้ำแทนการวนลูปต้นไม้ด้วยตนเอง มันจะค้นหาต้นไม้ย่อยทั้งหมด.
  • วนลูปลูกโดยตรง ด้วย for child in node เมื่อคุณต้องการเฉพาะลูกโดยตรงเท่านั้น.
  • จัดการการเข้ารหัสบน Windows: บน Windows, sys.stdout อาจใช้หน้าโค้ดเก่า เพิ่ม sys.stdout.reconfigure(encoding="utf-8", errors="replace") เมื่อเริ่มต้นเมื่อพิมพ์ข้อความ Unicode.
  • ติดตั้ง [pdf] เพิ่มเติม: อย่า import SaveFormat.Pdf ฟังก์ชันการทำงานโดยไม่ได้ติดตั้งก่อน pip install "aspose-note[pdf]". หากไม่มี ReportLab การบันทึกเป็น PDF จะทำให้เกิดข้อผิดพลาดการ import ในขณะรันไทม์.
 ภาษาไทย