วิธีอ่านข้อมูลเมตาของรูปภาพจาก OneNote ด้วย Python
ทุกโหนด 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 | ข้อความแทนสำหรับการเข้าถึง (body). None หากไม่ได้ตั้งค่า. |
img.AlternativeTextTitle | str | None | ชื่อเรื่องข้อความแทนสำหรับการเข้าถึง. 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: อ่านเมตาดาต้าสำหรับแต่ละภาพ
ตรวจสอบฟิลด์ที่เป็น nullable ทั้งหมดด้วย 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เป็นตัวสำรอง.- มิติอยู่ใน points (1 point = 1/72 นิ้ว), ตรงกับแนวปฏิบัติของ PowerPoint และ PDF.