python docx set table cell background and text color

前端 未结 6 761
故里飘歌
故里飘歌 2021-01-12 20:51

I am using python 2.7 with docx and I would like to change the background and text color of cells in my table based on condition.

I could not find any usefull resour

6条回答
  •  一向
    一向 (楼主)
    2021-01-12 21:30

    Taking from Nikos Tavoularis answer I would just change the shading_elm_1 declaration, as if you include the cell color in a loop for instance things might get messy.

    As such, my suggestion would be:

    from docx.oxml.ns import nsdecls
    from docx.oxml import parse_xml
    
    table.rows[0].cells[0]._tc.get_or_add_tcPr().append(parse_xml(r''.format(nsdecls('w'))))
    

提交回复
热议问题