KingT753
KingT753

Reputation: 81

How to change font size for all cells in a table with no text?

I've successfully created a blank table in a Presentation (7x9) and currently all cells are blank. Before I put anything in them, I want to change the font size (currently 18) to 10.5. While someone has already asked/answered this question, and I've tried these solutions (see below), I'm still not getting the result I want (font in 10.5 in every cell in a blank table).

demographics_table_slide_placeholder_title = 
demographics_table_slide.placeholders[0]
demographics_table_slide_placeholder_title.text = 'Demographics Table'
x = Inches(0.25)
y = Inches(1.625)
cx = Inches(9.56)
cy = Inches(3.72)
demographics_table = 
demographics_table_slide.shapes.add_table(7,9,x,y,cx,cy).table

Code that works to change one single cell:

cell = demographics_table.rows[0].cells[0]
paragraph = cell.text_frame.paragraphs[0]
paragraph.font.size = Pt(10.5)

Code that does not work to change all cells in the blank table:

def iter_cells(demographics_table):
    for row in demographics_table.rows:
        for cell in row.cells:
            yield cell

for cell in iter_cells(demographics_table):
    for paragraph in cell.text_frame.paragraphs:
        for run in paragraph.runs:
            run.font.size = Pt(10.5)

I expected all cells to be changed to size 10.5 font but no changes were made when trying to iterate through this code. Thanks to any help!

Upvotes: 3

Views: 802

Answers (1)

scanny
scanny

Reputation: 28903

Each cell in a newly-created table will have a single paragraph. However, each of those paragraphs will have zero runs. So this line of your code will never execute:

run.font.size = Pt(10.5)

because for run in paragraph.runs: iterates zero times for each paragraph.

Try this instead:

for cell in iter_cells(demographics_table):
    for paragraph in cell.text_frame.paragraphs:
        paragraph.font.size = Pt(10.5)

or more compactly but less flexibly (to other situations):

for cell in iter_cells(demographics_table):
    cell.text_frame.paragraphs[0].font.size = Pt(10.5)    

Upvotes: 3

Related Questions