Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a table in python docx and bolding text

Tags:

python-docx

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

like image 808
KerryLee Avatar asked Apr 27 '16 15:04

KerryLee


1 Answers

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
like image 84
scanny Avatar answered Nov 01 '22 07:11

scanny