首页 > 解决方案 > 为什么在我只尝试分配变量时执行操作?

问题描述

我正在尝试重新组织以下代码

from pptx import Presentation
from pptx.dml.color import RGBColor
from pptx.enum.chart import XL_LABEL_POSITION
from pptx.util import Pt

prs = Presentation('test.pptx')
for slide in prs.slides:
    for shape in slide.shapes:
        if not shape.has_chart:
            continue
        chart = shape.chart
        for series in chart.series:      
            for point, value in zip(series.points, series.values):
                if value >= 7.5:
                    fill = point.format.fill
                    fill.solid()
                    fill.fore_color.rgb = RGBColor(0, 176, 80)
                elif value < 7.5 and value >= 3.5:
                    fill = point.format.fill
                    fill.solid()
                    fill.fore_color.rgb = RGBColor(255, 192, 0)
                    data_label = point.data_label
                    data_label.position = XL_LABEL_POSITION.CENTER
                    font = data_label.font
                    font.color.rgb = RGBColor(0, 0, 0)
                    font.size = Pt(9)
                elif value <3.5 and value > 1:
                    fill = point.format.fill
                    fill.solid()
                    fill.fore_color.rgb = RGBColor(255, 0, 0)
prs.save('testoutput.pptx')

进入以下代码

from pptx import Presentation
from pptx.dml.color import RGBColor
from pptx.enum.chart import XL_LABEL_POSITION
from pptx.util import Pt

prs = Presentation('test.pptx')
for slide in prs.slides:
    for shape in slide.shapes:
        if not shape.has_chart:
            continue
        chart = shape.chart
        for series in chart.series:
            for point, value in zip(series.points, series.values):          
                fill = point.format.fill
                fill.solid()
                data_label = point.data_label
                font = data_label.font
                if value >= 7.5:                    
                    fill.fore_color.rgb = RGBColor(0, 176, 80)
                elif value < 7.5 and value >= 3.5:                    
                    fill.fore_color.rgb = RGBColor(255, 192, 0)                    
                    data_label.position = XL_LABEL_POSITION.CENTER                    
                    font.color.rgb = RGBColor(0, 0, 0)
                    font.size = Pt(9)
                elif value <3.5 and value > 1:                    
                    fill.fore_color.rgb = RGBColor(255, 0, 0)
prs.save('testoutput.pptx')

其中变量被预先分配,然后在其余的 if 语句(填充、数据标签、字体)中使用。对我来说,代码中的一切看起来都不错,但我显然遗漏了一些东西,因为输出不同:

正在操作的原始幻灯片 (test.pptx)

在此处输入图像描述

代码1的正确输出:

在此处输入图像描述

代码 2 的输出不正确:

在此处输入图像描述

标签: pythonpowerpoint

解决方案


也许与直觉相反,一个系列“点”(通常)在您访问它之前并不存在。因此,对点进行迭代是一种破坏性操作,因为某些图表标记(在本例中为条形)属性的继承被破坏了。

PowerPoint 通过在创建时将所有当前值分配给该点来解决此问题。在 UI 中,通过选择单个栏并对其进行编辑来创建一个点。

因此,您需要为您访问的每个点分配某些属性。您可以通过单独遍历值然后仅访问您将要更改的点来对此进行选择。

也许是这样的:

for idx, value in enumerate(series.values):          
    if value < 1:
        continue
    point = series.points[idx]
    # --- do pointy operations ---                

但是,一旦您访问该点,您就需要设置所有您不希望使用默认值的属性。


推荐阅读