问题
Working on a table in python-docx. I am trying to apply a different style to the first row of the table vs. the rest of the rows. This would be the Header row. Here's the table:
table2 = doc.add_table(rows=10, cols=3)
heading_cells_table2 = table2.rows[0].cells
heading_cells_table2[0].text = 'Header 1'
heading_cells_table2[1].text = 'Header 2'
heading_cells_table2[2].text = 'Header 3'
I was hoping I could do something like this:
table2.rows[0].style = table_heading_style
I've tried a bunch of other variations and combinations of .add_run(), .cells, .font, .text, etc. with no luck.
Is it possible to change formatting for individual cells/rows/columns in a table? If so, how is it done?
Thanks for the help.
EDIT
For a specific example of the answer to this problem, here's how I ended up Centering the text in 2 out of 3 columns in a table, thanks to scanny's help.
for column in itertools.islice(table2.columns, 1, 3):
for cell in column.cells:
for paragraph in cell.paragraphs:
paragraph.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
回答1:
A paragraph style can only be applied to a paragraph.
for paragraph in cell.paragraphs:
paragraph.style = document.styles['Heading 1']
来源:https://stackoverflow.com/questions/44874327/python-docx-how-to-apply-different-styles-to-different-cells-in-a-table