छवियां और संलग्न फ़ाइलें — 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 | `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)इमेज के 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 | `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 |
|---|---|---|
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समर्थित नहीं है।.