使用python读取MS Word文件中的自定义文档属性

使用python读取MS Word文件中的自定义文档属性

问题描述:

如何使用python获取MS-Word 2010文档的文档属性?

How can I get the document properties of a MS-Word 2010 Document using python?

具有文档属性的意思是那些可以在FILE-> Info-> Properties-> Advanced properties(在MS-WORD 2010中)下添加或修改的人

with document properties i mean those who can be added or modified under FILE -> Info-> Properties-> Advanced properties (In MS-WORD 2010)

我正在Windows764bit上使用python 2.7和相应的pywin32com版本来访问文档文件...

I'm using python 2.7 on windows764bit and the corresponding pywin32com version to access the doc-file...

我发现具有 value name 方法的 CustomProperty 对象似乎对我而言是正确的选择( http://msdn.microsoft.com/en-us/library/bb257518%28v = office.12%29.aspx )

I found the CustomProperty-object with the methods value and name witch seem to be the right thing for my purpose (http://msdn.microsoft.com/en-us/library/bb257518%28v=office.12%29.aspx)

但是我不知道如何在python中实现类成员...

But I dont know how to implement the class members in python...

我想做的事情是获取手动指定的属性,例如作者,版本...

the thing i want to do is to get manually specified properties like author, version...

我自己解决了...

一种读取自定义文档属性的方法是:

A way to read the custom document properties is:

import win32com.client as win32
word = win32.Dispatch("Word.Application")
word.Visible = 0
doc = word.Documents.Open(file)
try:
    csp= doc.CustomDocumentProperties('property_you_want_to_know').value
    print('property is %s' % csp)

except exception as e:
    print ('\n\n', e)

doc.Saved= False
doc.Save()
doc.Close()

word.Quit()