python docx set table cell background and text color

前端 未结 6 763
故里飘歌
故里飘歌 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:19

    If you want to loop through the cells in a row use:

    def color_row(row=0):
        'make row of cells background colored, defaults to column header row'
        row = t.rows[row]
        for cell in row.cells:
            shading_elm_2 = parse_xml(r''.format(nsdecls('w')))
            cell._tc.get_or_add_tcPr().append(shading_elm_2)
    

    run the function to color cells in the second row

    color_row(2)

提交回复
热议问题