نمای کلی ویژگیها — Aspose.Note FOSS برای Python
Aspose.Note FOSS for Python (package aspose-note, نسخه ۲۶.۳.۱) یک API Python برای خواندن 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) فراهم میکند- بارگذاری مبتنی بر جریان، عملیات I/O دیسک را برای جریانهای کاری در حافظه یا شبکه حذف میکند
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)پیمایش DOM سند
سند کامل OneNote به صورت درختی از اشیای Python تایپشده نمایش داده میشود. هر گره از Node یا CompositeNode:
Document: ریشه؛ نمایش میدهد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}")استخراج محتوای متن غنی
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 الگوی بازدیدکننده را برای پیمایش ساختاری تمام سند فراهم میکند. هر کدام را بازنویسی کنید 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 در نسخه v26.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()مرجع فرمتها و شمارشها
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, و بیشتر فیلدهای metadata میتوانندNone. همیشه باif x is not Noneیاif xقبل از دسترسی به ویژگیها. - استفاده کنید
GetChildNodes(Type)برای جستجوی بازگشتی به جای پیمایش دستی درخت استفاده کنید. این روش تمام زیردرخت را جستجو میکند. - پیمایش فرزندان مستقیم با
for child in nodeوقتی فقط به فرزندان مستقیم نیاز دارید. - مدیریت رمزگذاری در ویندوز: در ویندوز،,
sys.stdoutممکن است از صفحه کدهای قدیمی استفاده کند. اضافه کنیدsys.stdout.reconfigure(encoding="utf-8", errors="replace")در زمان راهاندازی هنگام چاپ متن یونیکد. - نصب
[pdf]اضافی: وارد نکنیدSaveFormat.Pdfقابلیت بدون نصب اولیهpip install "aspose-note[pdf]". بدون ReportLab، ذخیره به PDF در زمان اجرا خطای import ایجاد میکند.