如何更改 table 中没有文本的所有单元格的字体大小?

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

我已经在演示文稿 (7x9) 中成功创建了一个空白 table,目前所有单元格都是空白的。在将任何内容放入其中之前,我想将字体大小(当前为 18)更改为 10.5。虽然有人已经 asked/answered 这个问题,并且我已经尝试了这些解决方案(见下文),但我仍然没有得到我想要的结果(空白的每个单元格中的字体为 10.5 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

用于更改单个单元格的代码:

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

无法更改空白中所有单元格的代码 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)

我希望所有单元格都更改为 10.5 号字体,但在尝试遍历此代码时没有进行任何更改。感谢任何帮助!

newly-created table 中的每个单元格都有一个段落。但是,这些段落中的每一个都将有 次运行。所以你的这行代码永远不会执行:

run.font.size = Pt(10.5)

因为 for run in paragraph.runs: 对每个段落重复零次。

试试这个:

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

或更紧凑但不太灵活(对其他情况):

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