Search code examples
pythondocxpython-docx

How change table contents and style using python-docx?


I found python-docx, it looks very smart, but I have to do some tasks that are not well documented.

I need to open a .docx template, with a table within, ad for all the istances present in a list previously created, I have to format them in the table inside the template.


Solution

  • Probably I've found a solution. It depends of document.xpath, a way to take a map of it is decompress the .docx and read the ./word/document.xml file.

    PATH_CELL = 'the path you individuate in document.xml'
    
    docbody = document.xpath('/w:document/w:body'+PATH_CELL, 
    namespaces=nsprefixes)[0]
    
    print 'Replacing ...',
    docbody = replace(docbody,'Welcome','Hello') 
    

    I've found this way to run the game. Any else ?