छवियां और संलग्न फ़ाइलें — Aspose.Note FOSS for 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`strNone`
Bytesbytesकच्चा छवि डेटा (फ़ॉर्मेट मूल के समान है, जैसे PNG, JPEG)
Width`floatNone`
Height`floatNone`
AlternativeTextTitle`strNone`
AlternativeTextDescription`strNone`
HyperlinkUrl`strNone`
Tagslist[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)

इमेज के Alt टेक्स्ट और हाइपरलिंक्स की जाँच करें

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)

अटैच्ड फ़ाइलें निकालना

Embedded file attachments इस रूप में संग्रहीत होते हैं 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`strNone`
Bytesbytesकच्ची फ़ाइल सामग्री
Tagslist[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}")

सारांश: इमेज बनाम अटैच्डफ़ाइल

फ़ीचरImageAttachedFile
FileNameहाँ (मूल छवि नाम)हाँ (मूल फ़ाइल नाम)
Bytesकच्चे इमेज बाइट्सकच्चे फ़ाइल बाइट्स
Width / Heightहाँ (रेंडर किए गए आयाम)नहीं
AlternativeTextTitle/Descriptionहाँनहीं
HyperlinkUrlहाँनहीं
Tagsहाँहाँ
Replace(node) विधिहाँनहीं

टिप्स

  • हमेशा सावधान रहें None फ़ाइलनाम।. img.FileName है None जब फ़ाइल का स्रोत दस्तावेज़ में कोई नाम नहीं था।.
  • img.Bytes कभी नहीं None और हमेशा raw binary content है; यह प्लेसहोल्डर छवियों के लिए शून्य बाइट्स हो सकता है।.
  • उपयोग करें Page.GetChildNodes(Image) के बजाय Document.GetChildNodes(Image) एकल पृष्ठ तक निष्कर्षण को सीमित करने के लिए।.
  • यह Image.Replace(image) विधि एक इमेज नोड की सामग्री को in-memory में दूसरे से बदल देती है; saving back to .one समर्थित नहीं है।.

See Also

 हिन्दी