תמונות וקבצים מצורפים — Aspose.Note FOSS עבור 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)חילוץ קבצים מצורפים
קבצים מצורפים משובצים נשמרים כ 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
| תכונה | Image | AttachedFile |
|---|---|---|
FileName | כן (שם התמונה המקורי) | כן (שם הקובץ המקורי) |
Bytes | בייטים גולמיים של תמונה | בייטים גולמיים של קובץ |
Width / Height | כן (ממדים מוצגים) | לא |
AlternativeTextTitle/Description | כן | לא |
HyperlinkUrl | כן | לא |
Tags | כן | כן |
Replace(node) שיטה | כן | לא |
טיפים
- תמיד להגן מפני
Noneשמות קבצים.img.FileNameהואNoneכאשר לקובץ לא היה שם במסמך המקור. img.Bytesאינו אף פעםNoneוְהוּא תָמִיד תּוֹכֶן בִּינָרִי גָּלוּי; יָכוֹל לִהְיוֹת אפס בתים עבור תמונות מציינות מקום.- השתמש
Page.GetChildNodes(Image)במקוםDocument.GetChildNodes(Image)כדי להגביל את החילוץ לעמוד יחיד. - ה
Image.Replace(image)המתודה מחליפה את תוכן של צומת תמונה אחת באחר בזיכרון; שמירה חזרה ל.oneאינה נתמכת.