4

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

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

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

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)

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...

2 Answers 2

8

I solved it by myself...

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the post. it helped me. minor change, i think it should : "doc = word.Documents.Open(file)" and following line gave me error while running so i commented doc.Saved= False doc.Save() doc.Close()
1

For later Word documents (docx) also module python-docx can be used for reading or setting of document properties:

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)  

1 Comment

Sorry -1 as the question is about custom properties, not core properties. Docx unfortunately does not support custom properties yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.