راهنمای توسعهدهنده
Aspose.Note FOSS for Python is a free, open-source library for reading Microsoft OneNote .one فایلهای بخش بدون هیچ وابستگیای به Microsoft Office. این یک API عمومی تمیز تحت aspose.note بسته، که بر پایه Aspose.Note برای رابط .NET مدلسازی شده است. این کتابخانه برای خودکارسازی اسناد، ایندکسگذاری محتوا، خطوط لوله استخراج داده و جریانهای کاری بایگانی مناسب است.
این راهنمای توسعهدهنده تمام سطح API عمومی موجود در نسخهٔ ۲۶.۳.۱ را پوشش میدهد، به همراه مثالهای کد قابل اجرا برای هر ویژگی اصلی.
بارگذاری سند
یک را بارگذاری کنید .one فایل از مسیر فایل یا یک جریان باینری. این 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 | `Title | None` |
Author | `str | None` |
CreationTime | `datetime | None` |
LastModifiedTime | `datetime | None` |
Level | `int | None` |
استخراج متن
استخراج تمام متن ساده
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 برای پیادهسازی یک بازدیدکننده که کل درخت سند را پیمایش میکند:
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 فیلدها وجود دارند اما در نسخه v26.3.1 به صادرکننده PDF ارسال نمیشوند. کل سند همیشه صادر میشود.
محدودیتهای فعلی
| منطقه | وضعیت |
|---|---|
خواندن .one فایلها | کاملاً پشتیبانی میشود |
| صادرات PDF (از طریق ReportLab) | پشتیبانی میشود |
نوشتن به .one | پیادهسازی نشده |
| اسناد رمزگذاریشده | پشتیبانی نمیشود (خطا میاندازد IncorrectPasswordException) |
| قالبهای ذخیرهسازی HTML / تصویر / ONE | اعلام شده برای سازگاری API؛ خطا میاندازد UnsupportedSaveFormatException |
راهنماهای موجود
- مروری بر ویژگیها: فهرست کامل ویژگیها با شواهد
- شروع کار: پیشنیازها، نصب، و گامهای اولیه
- نصب: pip install و وابستگیهای اختیاری