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

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

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


先决条件

pip install aspose-note

图像属性

PropertyTypeDescription
img.Bytesbytes原始图像数据。使用 open(name, "wb").write(img.Bytes) 写入磁盘。
img.FileNamestr | None原始文件名存储在 .one 文件中。如果未存储,则 None
img.Widthfloat | None以点为单位的显示宽度。如果未存储,则 None
img.Heightfloat | None以点为单位的显示高度。如果未存储,则 None
img.AlternativeTextDescriptionstr | None可访问性替代文本正文。如果未设置,则 None
img.AlternativeTextTitlestr | None可访问性替代文本标题。如果未设置,则 None
img.HyperlinkUrlstr | None如果图像是可点击的超链接,则为 URL。如果未链接,则 None
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}")

带 alt 文本的图像

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 的约定。

另请参阅

 中文