4

Python を使用して MS-Word 2010 ドキュメントのドキュメント プロパティを取得するにはどうすればよいですか?

ドキュメント プロパティとは、ファイル -> 情報 -> プロパティ -> 詳細プロパティ (MS-WORD 2010) で追加または変更できるユーザーを意味します。

Windows764ビットでpython 2.7を使用しており、対応するpywin32comバージョンを使用してドキュメントファイルにアクセスしています...

メソッドの名前の魔女を持つCustomPropertyオブジェクトが、私 の目的に適しているようです ( 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 に答える