我已经在演示文稿 (7x9) 中成功创建了一个空白表格,目前所有单元格都是空白的。在将任何内容放入其中之前,我想将字体大小(当前为 18)更改为 10.5。虽然有人已经问/回答了这个问题,并且我已经尝试了这些解决方案(见下文),但我仍然没有得到我想要的结果(空白表格中每个单元格的字体为 10.5)。
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)
无法更改空白表格中所有单元格的代码:
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 号字体,但在尝试遍历此代码时没有进行任何更改。感谢任何帮助!
请您参考如下方法:
新创建的表格中的每个单元格都有一个段落。但是,这些段落中的每一个都将有零 运行。所以你的这行代码永远不会执行:
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)
