Python में OneNote से संलग्न फ़ाइलें कैसे सहेजें

Python में OneNote से संलग्न फ़ाइलें कैसे सहेजें

OneNote .one फ़ाइलों में एम्बेडेड फ़ाइल अटैचमेंट हो सकते हैं: कोई भी फ़ाइल प्रकार जो OneNote में Insert → File Attachment का उपयोग करके पृष्ठ में डाली गई थी। 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 में वापस नहीं लिखता। आप अटैचमेंट्स नहीं बना या संशोधित कर सकते।

संबंधित देखें

 हिन्दी