نظرة عامة على الميزات — Aspose.Note FOSS لـ Python
Aspose.Note FOSS for Python (package aspose-note, الإصدار 26.3.1) يوفر واجهة برمجة تطبيقات 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)- التحميل القائم على التدفق يلغي عمليات الإدخال/الإخراج على القرص لتدفقات العمل في الذاكرة أو عبر الشبكة
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 في الإصدار 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()مرجع الصيغ والعدادات
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]إضافي: لا تستوردSaveFormat.Pdfالوظيفة دون تثبيت أولاًpip install "aspose-note[pdf]". بدون ReportLab، سيؤدي حفظ إلى PDF إلى رفع خطأ استيراد أثناء التشغيل.