Search code examples
pythonpython-docx

How can I make table header cells both bold and underline in python?


I am creating a table using python 3.4 and I would like to make the header both bold and underline. The following code will make the header bold:

table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].paragraphs[0].add_run('Date Filmed:').bold = True
hdr_cells[2].paragraphs[0].add_run('Barcode Number:').bold = True

If I change the 3rd line to:

hdr_cells[0].paragraphs[0].add_run('Date Filmed:').underline = True

it will make the text underlined, but not bold. It there a way to make the text of the header both bold and underlined?


Solution

  • You just need to add boolean run properties one at a time

    run = hdr_cells[0].paragraphs[0].add_run('Date Filmed:')
    run.bold = True
    run.underline = True