读写 Windows "tags" with Python 3

Reading and writing Windows "tags" with Python 3

在Windows 中可以标记图像文件。可以通过右键单击文件,单击“详细信息”选项卡,然后单击“标记”属性 值单元格来查看和编辑这些标记。

我希望能够使用 Python 3.

读写这些标签

这不是 EXIF 数据,因此 EXIF 解决方案不起作用。我相信它是 Windows 属性 系统的一部分,但我在开发中心找不到参考。我查看了 win32com.propsys,也没有看到任何东西。

我以前写过一个程序可以做到这一点,但后来我把它弄丢了,所以我知道这是可能的。以前我是在没有 pywin32 的情况下完成的,但任何解决方案都会很棒。我想我用过windll,但我记不起来了。

这是一些使用 IPropertyStore interfacepropsys 的示例代码:

import pythoncom
from win32com.propsys import propsys
from win32com.shell import shellcon

# get PROPERTYKEY for "System.Keywords"
pk = propsys.PSGetPropertyKeyFromName("System.Keywords")

# get property store for a given shell item (here a file)
ps = propsys.SHGetPropertyStoreFromParsingName("c:\path\myfile.jpg", None, shellcon.GPS_READWRITE, propsys.IID_IPropertyStore)

# read & print existing (or not) property value, System.Keywords type is an array of string
keywords = ps.GetValue(pk).GetValue()
print(keywords)

# build an array of string type PROPVARIANT
newValue = propsys.PROPVARIANTType(["hello", "world"], pythoncom.VT_VECTOR | pythoncom.VT_BSTR)

# write property
ps.SetValue(pk, newValue)
ps.Commit()

此代码对于任何 Windows 属性.

都非常通用

我正在使用 System.Keywords,因为它对应于您在 属性 sheet.[=16= 中看到的 jpeg 的 "tags" 属性 ]

并且代码适用于 jpeg 和其他格式的 reading (GetValue) 属性,但并非所有 Windows 编解码器都支持 属性 写入 (SetValue),例如,它无法将扩展属性写回 .png。