Creating a table in python docx and bolding text

KerryLee picture KerryLee · Apr 27, 2016 · Viewed 12.9k times · Source
doc=Document()
table = doc.add_table(rows = 13, cols = 5)
table.style = 'Table Grid'
row = table.rows[0]
row.cells[0].text = ('text').bold

I am trying to create a table and bold the text but cannot get the syntax right

Answer

scanny picture scanny · Apr 27, 2016

The .text method on a cell just sets the cell contents in "plain text". If you want to format the font of that text (like make it bold), you have to access the text at the run level. Something like this will work but best for you to dig into the documentation a bit more and understand why :) http://python-docx.readthedocs.org/en/latest/user/text.html#apply-character-formatting

cell = row.cells[0]
cell.text = "text"
run = cell.paragraphs[0].runs[0]
run.font.bold = True