如何在 Python 中读取 OneNote 的图像元数据

如何在 Python 中读取 OneNote 的图像元数据

每个 Image OneNote 文档中的节点在原始像素字节旁携带元数据:原始文件名、显示尺寸(宽度和高度,以点为单位)、用于可访问性的替代文本,以及(如果图像被链接)可选的超链接 URL。Aspose.Note FOSS for Python 通过 Image 类。.


先决条件

pip install aspose-note

图像属性

属性类型描述
img.Bytesbytes原始图像数据。写入磁盘时使用 open(name, "wb").write(img.Bytes).
img.FileName`strNone`
img.Width`floatNone`
img.Height`floatNone`
img.AlternativeTextDescription`strNone`
img.AlternativeTextTitle`strNone`
img.HyperlinkUrl`strNone`
img.Tagslist[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 点 = 1/72 英寸),符合 PowerPoint 和 PDF 的约定。.

另请参阅

 中文