I have a table where there are two separate texts in a single cell.
eg.
-------------
| r1 | r2 |
| 25 | 32 |
-------------
| s1 | s2 |
| 23 | 56 |
-------------
| 5734.23 |
-------------
Let's consider this is the table, I want to change font size and color for r1,r2,s1 and s2 which will be grey and 8pts size, and for their values I want the same font but different color black or white and size will 9pts, and the bottom cell will be in bold and different font size from all of this 12pts.
I am able to put the values and change the font for a cell but in that same cell I want to use two different fonts and the font properties will be different too. Can anyone help me with this?
A _Cell
object has a .text_frame
property, like a "regular" geometric shape (auto-shape), and it works the same way, providing a reference to a TextFrame
object.
You can access the paragraphs in the cell with cell.text_frame.paragraphs
, call .add_paragraph()
on the TextFrame
object, and add runs with different character formatting to a paragraph.
It's up to you whether you make these different paragraphs or different runs, but that's the basic idea. Note that each cell in a freshly-created table contains a single paragraph containing no runs.
For example:
cell = table.cell(0, 0)
text_frame = cell.text_frame
paragraph = text_frame.paragraphs[0]
run = paragraph.add_run()
run.text = "r1"
run.bold = True
run_2 = paragraph.add_run()
run.text = "25"
run.italic = True
You don't need to spell each step out on its own line; for example, you could do this to get the paragraph:
paragraph = table.cell(0, 0).text_frame.paragraphs[0]
I just spell out the steps here so you can more clearly see each one. You can compose them into lines as you see fit.