كيفية حفظ الملفات المرفقة من OneNote باستخدام Python
يمكن لملفات OneNote .one أن تحتوي على مرفقات ملفات مدمجة: أي نوع ملف تم إدراجه في صفحة باستخدام Insert → File Attachment في OneNote. تُظهر Aspose.Note FOSS for Python هذه من خلال الفئة AttachedFile، التي توفر اسم الملف الأصلي والبايتات الخام للملف المدمج.
المتطلبات المسبقة
pip install aspose-noteالخطوة 1: تحميل المستند
from aspose.note import Document
doc = Document("MyNotes.one")الخطوة 2: العثور على جميع الملفات المرفقة
استخدم GetChildNodes(AttachedFile) لجمع كل مرفق في المستند بشكل متكرر، بغض النظر عن الصفحة أو المخطط الذي يظهر فيه:
from aspose.note import Document, AttachedFile
doc = Document("MyNotes.one")
attachments = doc.GetChildNodes(AttachedFile)
print(f"Found {len(attachments)} attachment(s)")الخطوة 3: حفظ كل مرفق على القرص
الوصول إلى af.Bytes للحصول على محتوى الملف الخام و af.FileName للاسم الأصلي. احرص دائمًا على تجنب اسم ملف None: تُعيد المكتبة None عندما لا يتم تخزين بيانات اسم الملف في الملف:
from aspose.note import Document, AttachedFile
doc = Document("MyNotes.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} ({len(af.Bytes):,} bytes)")مثال كامل
يقوم هذا البرنامج النصي باستخراج جميع المرفقات من ملف .one وحفظها في دليل إخراج مخصص:
from pathlib import Path
from aspose.note import Document, AttachedFile
def save_all_attachments(one_path: str, out_dir: str = "attachments") -> None:
doc = Document(one_path)
out = Path(out_dir)
out.mkdir(exist_ok=True)
attachments = doc.GetChildNodes(AttachedFile)
if not attachments:
print("No attachments found.")
return
for i, af in enumerate(attachments, start=1):
name = af.FileName or f"attachment_{i}.bin"
dest = out / name
dest.write_bytes(af.Bytes)
print(f" [{i}] {name} ({len(af.Bytes):,} bytes)")
print(f"\nSaved {len(attachments)} file(s) to '{out_dir}/'")
save_all_attachments("MyNotes.one")ملاحظات
af.Bytesتُعيدb""(بايتات فارغة) عندما لا يمكن تحليل بيانات المرفق من الملف الثنائي. تحقق منlen(af.Bytes) > 0قبل الحفظ إذا كنت تريد تخطي المرفقات الفارغة.af.Tagsهي قائمة من كائناتNoteTagإذا كان للمرفق أي علامات OneNote مطبقة عليه.- Aspose.Note FOSS for Python يقرأ ملفات
.oneلكنه لا يكتب مرة أخرى إلى.one. لا يمكنك إنشاء أو تعديل المرفقات.