ภาพรวมของคุณลักษณะ — 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,FileFormatPage: ลูกโดยตรงของDocument; เปิดเผยTitle,Author,CreationTime,LastModifiedTime,LevelTitle: เปิดเผยTitleText,TitleDate,TitleTime(ทั้งหมดRichTextโหนด)Outline: ตัวคอนเทนเนอร์เชิงตำแหน่งที่มีHorizontalOffset,VerticalOffset,MaxWidth,MaxHeight,MinWidth,ReservedWidth,IndentPositionOutlineElement: ตัวคอนเทนเนอร์ใบไม้; เปิดเผย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 มี:
| คุณสมบัติ | ประเภท | คำอธิบาย |
|---|---|---|
Text | str | ข้อความส่วน |
Style | TextStyle | เมตาดาต้าการจัดรูปแบบ |
TextStyle คุณสมบัติ:
| คุณสมบัติ | ประเภท |
|---|---|
IsBold, IsItalic, IsUnderline, IsStrikethrough | bool |
IsSuperscript, IsSubscript | bool |
FontName | `str |
FontSize | `float |
FontColor, Highlight | `int |
Language | `int |
IsHyperlink | bool |
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 | `str | None` |
Bytes | bytes | ข้อมูลภาพดิบ |
Width, Height | `float | None` |
AlternativeTextTitle | `str | None` |
AlternativeTextDescription | `str | None` |
HyperlinkUrl | `str | None` |
Tags | list[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 | `str | None` |
Bytes | bytes | ข้อมูลไฟล์ดิบ |
Tags | list[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 เปิดเผยโครงสร้างตารางทั้งหมด:
| คลาส | คุณสมบัติหลัก |
|---|---|
Table | Columns: 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 | `int | None` |
Label | `str | None` |
FontColor | `int | None` |
Highlight | `int | None` |
CreationTime | `datetime | None` |
CompletedTime | `datetime | None` |
เมธอดแฟคทอรี: 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 | `str | None` |
Restart | `int | None` |
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
| ตัวเลือก | ประเภท | คำอธิบาย |
|---|---|---|
PageIndex | int | ฟิลด์มีอยู่; ไม่ได้ส่งต่อไปยังตัวส่งออก PDF ในเวอร์ชัน 26.3.1 (ไม่มีผล) |
PageCount | `int | None` |
ImageCompression | `Any | None` |
JpegQuality | `int | None` |
PageSettings | `Any | None` |
PageSplittingAlgorithm | `Any | None` |
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]เพิ่มเติม: อย่า importSaveFormat.Pdfฟังก์ชันการทำงานโดยไม่ได้ติดตั้งก่อนpip install "aspose-note[pdf]". หากไม่มี ReportLab การบันทึกเป็น PDF จะทำให้เกิดข้อผิดพลาดการ import ในขณะรันไทม์.