有没有办法改变py pptx中table中所有单元格的颜色?

Is there a way to change the color of all of the cells in a table in py pptx?

我知道有一种方法可以更改 table 中单个单元格的颜色,方法是根据 table 命名该单元格,然后为该单元格着色;但是我正在寻找一种方法来同时更改一个 table 中所有单元格的颜色。

table 是 7x3,我已经问过关于更改 table 中所有单元格的字体大小的问题,所以我认为更改所有单元格颜色的方法是相似的对此。

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

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

for cell in iter_cells(urbanicity_table):
    for paragraph in cell.text_frame.paragraphs:
        paragraph.font.size = Pt(6)

table 使用此幻灯片所用模板 PowerPoint 的默认配色方案预先着色。请帮忙,谢谢!

PowerPoint 有一个 "table-style" 的概念,可以通过从其中一个功能区的图库中选择来应用于 table。 table 样式允许为 column-headings 和 row-headings 设置填充颜色和 font-formatting,并允许选择水平条带、垂直条带和单元格边框。

python-pptx 中还没有任何 API 支持将 table-style 应用到 table。因此,您提到的方法可能是实现相似外观的最简单方法,至少就单元格背景色而言是这样。

类似于:

from pptx.dml.color import RGBColor

for cell in iter_cells(table):
    fill = cell.fill
    fill.solid()
    fill.fore_color.rgb = RGBColor(255, 0, 0)