how can you use two different font styles and font properties in the same table cell using python?

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.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top