4

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

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

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

我发现 带有方法名称的CustomProperty -object对于我的目的似乎是正确的(http://msdn.microsoft.com/en-us/library/bb257518%28v=office.12%29.aspx )

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

我想要做的是获得手动指定的属性,如作者、版本......

4

2 回答 2

7

我自己解决了...

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

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()
于 2013-03-25T10:49:26.393 回答
0

对于以后的 Word 文档(docx),也python-docx可以使用模块来读取或设置文档属性:

from docx import Document

path = 'D:/myfile.docx'
# create document handler
document = Document(path)
# read properties
document_property = document.core_properties
# print all accessible document properties
print([p for p in dir(document_property) if not p.startswith('_')])
# print author of the document
print(document_property.author)
# set new value for title
document_property.title = 'Everything is Awesome'
# save changes to current file
document.save(path)  
于 2021-09-20T12:56:25.143 回答