كيفية حفظ الملفات المرفقة من OneNote في Python

كيفية حفظ الملفات المرفقة من OneNote في Python

OneNote .one يمكن أن تحتوي الملفات على ملفات مرفقة: أي نوع من المجلد تم إدخاله في صفحة باستخدام إدخال ملف الارتباط في OneNote. Aspose.Nota FOSS for Python يظهر هذا من خلال AttachedFile فئة، والتي توفر اسم الملف الأصلي والبايتات الخام للملف المدمج.


المعايير

pip install aspose-note

الخطوة الأولى: تحميل الوثيقة

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 reads .one الملفات ولكن لا يكتب مرة أخرى إلى .one.لا يمكنك إنشاء أو تعديل المرفقات.

انظر أيضا

 العربية