الصور والملفات المرفقة — Aspose.Note FOSS لـ Python
Aspose.Note FOSS for Python gives direct access to both embedded images and attached files stored inside OneNote .one ملفات الأقسام. يتم عرض المحتوى من خلال Image و AttachedFile أنواع العقد.
استخراج الصور
كل صورة في مستند OneNote يتم تمثيلها بـ Image عقدة. استخدم GetChildNodes(Image) لاسترجاع جميع الصور من المستند بشكل متكرر:
from aspose.note import Document, Image
doc = Document("MyNotes.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)")خصائص الصورة
| خاصية | نوع | وصف |
|---|---|---|
FileName | `str | None` |
Bytes | bytes | بيانات الصورة الخام (التنسيق يطابق الأصلي، مثل PNG، JPEG) |
Width | `float | None` |
Height | `float | None` |
AlternativeTextTitle | `str | None` |
AlternativeTextDescription | `str | None` |
HyperlinkUrl | `str | None` |
Tags | list[NoteTag] | علامات OneNote المرفقة بهذه الصورة |
حفظ الصور بأسماء ملفات آمنة
متى FileName هو None أو يحتوي على أحرف غير آمنة لنظام الملفات، قم بتنظيفها قبل الحفظ:
import re
from aspose.note import Document, Image
def safe_name(name: str | None, fallback: str) -> str:
if not name:
return fallback
return re.sub(r'[<>:"/\\|?*]', "_", name)
doc = Document("MyNotes.one")
for i, img in enumerate(doc.GetChildNodes(Image), start=1):
name = safe_name(img.FileName, f"image_{i}.bin")
with open(name, "wb") as f:
f.write(img.Bytes)استخراج الصور لكل صفحة
from aspose.note import Document, Page, Image
doc = Document("MyNotes.one")
for page_num, page in enumerate(doc.GetChildNodes(Page), start=1):
images = page.GetChildNodes(Image)
print(f"Page {page_num}: {len(images)} image(s)")
for i, img in enumerate(images, start=1):
filename = f"page{page_num}_img{i}.bin"
with open(filename, "wb") as f:
f.write(img.Bytes)فحص نص بديل الصورة والروابط التشعبية
from aspose.note import Document, Image
doc = Document("MyNotes.one")
for img in doc.GetChildNodes(Image):
if img.AlternativeTextTitle:
print("Alt title:", img.AlternativeTextTitle)
if img.AlternativeTextDescription:
print("Alt desc:", img.AlternativeTextDescription)
if img.HyperlinkUrl:
print("Linked to:", img.HyperlinkUrl)اكتشاف تنسيق ملف الصورة من البايتات
خاص بـ Python imghdr الوحدة (Python ≤ 3.12) أو الـ struct يمكن للوحدة اكتشاف تنسيق الصورة من البايتات الأولى:
from aspose.note import Document, Image
doc = Document("MyNotes.one")
for img in doc.GetChildNodes(Image):
b = img.Bytes
if b[:4] == b'\x89PNG':
fmt = "png"
elif b[:2] == b'\xff\xd8':
fmt = "jpeg"
elif b[:4] == b'GIF8':
fmt = "gif"
elif b[:2] in (b'BM',):
fmt = "bmp"
else:
fmt = "bin"
name = (img.FileName or f"image.{fmt}").rsplit(".", 1)[0] + f".{fmt}"
with open(name, "wb") as f:
f.write(b)استخراج الملفات المرفقة
يتم تخزين مرفقات الملفات المدمجة كـ AttachedFile العُقد:
from aspose.note import Document, AttachedFile
doc = Document("NotesWithAttachments.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)
print(f"Saved attachment: {filename} ({len(af.Bytes):,} bytes)")خصائص AttachedFile
| الخاصية | النوع | الوصف |
|---|---|---|
FileName | `str | None` |
Bytes | bytes | محتوى الملف الخام |
Tags | list[NoteTag] | علامات OneNote المرفقة بهذه العقدة |
فحص العلامات على الصور والمرفقات
كلاهما Image و AttachedFile العقد تدعم علامات OneNote:
from aspose.note import Document, Image, AttachedFile
doc = Document("MyNotes.one")
for img in doc.GetChildNodes(Image):
for tag in img.Tags:
print(f"Image tag: {tag.Label} completedTime={tag.CompletedTime}")
for af in doc.GetChildNodes(AttachedFile):
for tag in af.Tags:
print(f"Attachment tag: {tag.Label} completedTime={tag.CompletedTime}")ملخص: Image مقابل AttachedFile
| الميزة | Image | AttachedFile |
|---|---|---|
FileName | نعم (اسم الصورة الأصلي) | نعم (اسم الملف الأصلي) |
Bytes | بايتات الصورة الخام | بايتات الملف الخام |
Width / Height | نعم (الأبعاد المعروضة) | لا |
AlternativeTextTitle/Description | نعم | لا |
HyperlinkUrl | نعم | لا |
Tags | نعم | نعم |
Replace(node) طريقة | نعم | لا |
نصائح
- دائمًا احذر من
Noneأسماء الملفات.img.FileNameهوNoneعندما لم يكن للملف اسم في المستند الأصلي. img.Bytesليس أبدًاNoneوهو دائمًا المحتوى الثنائي الخام; يمكن أن يكون صفر بايت للصور النائبة.- استخدم
Page.GetChildNodes(Image)بدلاً منDocument.GetChildNodes(Image)لتحديد استخراج إلى صفحة واحدة. - ال
Image.Replace(image)الطريقة تستبدل محتوى عقدة صورة واحدة بأخرى في الذاكرة؛ حفظها مرة أخرى إلى.oneغير مدعوم.