כיצד לקרוא מטא‑נתוני תמונה מ‑OneNote בפייתון
כל צומת Image במסמך OneNote נושא מטא‑נתונים לצד בתים גולמיים של פיקסלים: שם הקובץ המקורי, ממדי התצוגה (רוחב וגובה בנקודות), טקסט חלופי לנגישות, ובאופן אופציונלי כתובת URL של קישור אם התמונה הייתה מקושרת. Aspose.Note FOSS for Python חושף את כל השדות האלה דרך המחלקה Image.
דרישות מקדימות
pip install aspose-noteמאפייני תמונה
| מאפיין | סוג | תיאור |
|---|---|---|
img.Bytes | bytes | נתוני תמונה גולמיים. כתוב לדיסק עם open(name, "wb").write(img.Bytes). |
img.FileName | str | None | שם הקובץ המקורי נשמר בקובץ .one. None אם לא נשמר. |
img.Width | float | None | רוחב תצוגה בנקודות. None אם לא נשמר. |
img.Height | float | None | גובה תצוגה בנקודות. None אם לא נשמר. |
img.AlternativeTextDescription | str | None | גוף טקסט alt לנגישות. None אם לא הוגדר. |
img.AlternativeTextTitle | str | None | כותרת טקסט alt לנגישות. None אם לא הוגדרה. |
img.HyperlinkUrl | str | None | כתובת URL אם התמונה היא קישור לחיצה. None אם לא מקושרת. |
img.Tags | list[NoteTag] | תגיות OneNote המצורפות לתמונה זו (כוכב, תיבת סימון, וכו’). |
שלב 1: טען את המסמך ומצא תמונות
from aspose.note import Document, Image
doc = Document("MyNotes.one")
images = doc.GetChildNodes(Image)
print(f"Found {len(images)} image(s)")שלב 2: קרא מטא‑נתונים עבור כל תמונה
הגן על כל השדות הניתנים לאפס עם is not None לפני השימוש:
from aspose.note import Document, Image
doc = Document("MyNotes.one")
for i, img in enumerate(doc.GetChildNodes(Image), start=1):
print(f"\nImage {i}:")
print(f" Filename: {img.FileName or '(no filename)'}")
print(f" Size: {img.Bytes and len(img.Bytes):,} bytes")
if img.Width is not None and img.Height is not None:
print(f" Dimensions: {img.Width:.1f} × {img.Height:.1f} pts")
if img.AlternativeTextDescription:
print(f" Alt text: {img.AlternativeTextDescription}")
if img.HyperlinkUrl:
print(f" Hyperlink: {img.HyperlinkUrl}")
if img.Tags:
for tag in img.Tags:
print(f" Tag: {tag.Label or tag.Icon}")דוגמה מלאה: שמירת תמונות עם דוח מטא‑נתונים
from pathlib import Path
from aspose.note import Document, Image
def report_and_save_images(one_path: str, out_dir: str = "images") -> None:
doc = Document(one_path)
images = doc.GetChildNodes(Image)
if not images:
print("No images found.")
return
out = Path(out_dir)
out.mkdir(exist_ok=True)
for i, img in enumerate(images, start=1):
# Determine save name
name = img.FileName or f"image_{i}.bin"
dest = out / name
# Save bytes
dest.write_bytes(img.Bytes)
# Report metadata
dims = (
f"{img.Width:.0f}×{img.Height:.0f}pts"
if img.Width is not None and img.Height is not None
else "unknown size"
)
alt = img.AlternativeTextDescription or ""
link = img.HyperlinkUrl or ""
print(f" [{i}] {name} {dims}"
+ (f" alt='{alt}'" if alt else "")
+ (f" url={link}" if link else ""))
print(f"\nSaved {len(images)} image(s) to '{out_dir}/'")
report_and_save_images("MyNotes.one")סינון תמונות לפי תכונה
תמונות עם קישורים
from aspose.note import Document, Image
doc = Document("MyNotes.one")
linked = [img for img in doc.GetChildNodes(Image) if img.HyperlinkUrl]
for img in linked:
print(f"{img.FileName or 'image'} → {img.HyperlinkUrl}")תמונות עם טקסט חלופי
from aspose.note import Document, Image
doc = Document("MyNotes.one")
with_alt = [img for img in doc.GetChildNodes(Image) if img.AlternativeTextDescription]
for img in with_alt:
print(f"{img.FileName}: {img.AlternativeTextDescription}")הערות
img.Bytesתמיד קיים (מחזירb""עבור תמונות לא קריאות, לעולם לאNone). בדוק אתlen(img.Bytes) > 0לפני השמירה.img.AlternativeTextTitleעשוי להיותNoneאם המסמך המקורי אינו מגדיר כותרת. השתמש בimg.AlternativeTextDescriptionכפתרון גיבוי.- הממדים הם בנקודות (נקודה אחת = 1/72 אינץ’), תואמים את הקונבנציות של PowerPoint ו‑PDF.