Search code examples
python-2.7python-docx

python-docx change 'Authors' meta-data


So, I've created a document with python-docx, and everything works as I'd expected, but the 'Authors' are shows as 'Cisco Employee'.

I've tried Googling 'python-docx change author', and other variations, but I can't find anything.

Does an option exist to change the authors name, or is that not possible currently?


Solution

  • Hmm, good to know. That's a bit of a bug. I'll get that fixed up :)

    In the meantime, you can create a template of your own and customize it to your needs.

    Make sure you study this page in the documentation: http://python-docx.readthedocs.org/en/latest/user/documents.html

    Basically what you do is:

    document = Document()
    document.save('my-new-template.docx')
    

    Then change 'my-new-template.docx' directly, using Word, to have the settings you want, and change your code to start like this:

    document = Document('my-new-template.docx')
    ...
    

    The changes you made to my-new-template will be reflected in all documents created that way.

    Longer term, perhaps soon, there will be a Document.core_props property that will provide access to a CoreProperties object. That object will allow you to set the author and other fields to whatever you want.

    UPDATE

    I added core properties support to python-docx, here's how you would use it:

    document = Document()
    core_properties = document.core_properties
    core_properties.author = 'Foo B. Baz'
    document.save('new-filename.docx')
    

    Details about the available properties are in the documentation here: http://python-docx.readthedocs.org/en/latest/api/document.html#coreproperties-objects