How do you retrieve the tags of a file in a list with Python (Windows Vista)?

烂漫一生 提交于 2019-11-27 05:13:11
JellicleCat

I went about it with the win32 extensions package, along with some demo code I found. I posted a detailed explanation of the process on this thread. I don't want to reproduce it all here, but here is the short version (click the foregoing link for the details).

  1. Download and install the pywin32 extension.
  2. Grab the code Tim Golden wrote for this very task.
  3. Save Tim's code as a module on your own computer.
  4. Call the property_sets method of your new module (supplying the necessary filepath). The method returns a generator object, which is iterable. See the following example code and output.

(This works for me in XP, at least.)

E.g.

import your_new_module
propgenerator= your_new_module.property_sets('[your file path]')
    for name, properties in propgenerator:
        print name
        for k, v in properties.items ():
            print "  ", k, "=>", v

The output of the above code will be something like the following:

DocSummaryInformation
   PIDDSI_CATEGORY => qux
SummaryInformation
   PIDSI_TITLE => foo
   PIDSI_COMMENTS => flam
   PIDSI_AUTHOR => baz
   PIDSI_KEYWORDS => flim
   PIDSI_SUBJECT => bar

Apparently, you need to use the Windows Search API looking for System.Keywords -- you can access the API directly via ctypes, or indirectly (needing win32 extensions) through the API's COM Interop assembly. Sorry, I have no vista installation on which to check, but I hope these links are useful!

It appears that Windows stores the tags in the files. Just tag any image and open the image in notepad and look for something XML-like(RDF) and you will find your tag there. Well... now we know that they are indeed stored in the files, but we still have no idea how to manipulate them.

But google is to the rescue. I googled: windows metadata api

and found this: http://blogs.msdn.com/pix/archive/2006/12/06/photo-metadata-apis.aspx

There are actually 2 different implentations of document properties (source).

  1. The COM implementation embeds them directly in the file itself : this is the approach used for Office documents for example. Tim Golden's code described on this page works well for these.

  2. On NTFS 5 (Win2k or later), you can add Summary info to any file, and it's stored in alternate data streams. I suppose the Windows Search API would work on these, but I have not tested it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!