读取 rectangle/texbox 的轮廓颜色

Read outline color of rectangle/texbox

我正在尝试读取矩形的轮廓颜色,但出现以下错误

A​​ttributeError: 没有 .rgb 属性 颜色类型 '_NoneColor'

我对不同的幻灯片使用相同的代码,结果如预期的那样出现。我确定形状有问题,但是我无法准确确定问题...有人可以帮忙吗?

对于 slide_3.shapes 中的形状:

if shape.name[:9] == 'Rectangle':

    shape_color = shape.fill.fore_color.rgb
    line_color = shape.line.color.rgb

颜色类型和填充类型有很多可能的组合。您需要从头开始,一路前行,边走边询问类型。

from pptx.enum.dml import MSO_COLOR_TYPE, MSO_FILL

def read_outline_color(shape):
    line_fill = shape.line.fill
    print("fill-type == %s" % line_fill.type)
    # ---we only handle solid, which is most common
    # ---the other common value is "background" which means no-fill
    if line_fill.type != MSO_FILL.SOLID:
        return

    # ---color can be specified as specific RGB color or a theme color
    # ---like ACCENT_1
    line_color = line_fill.fore_color
    print("color-type == %s" % line_color.type)
    if line_color.type == MSO_COLOR_TYPE.SCHEME:
        print("color == %s" % line_color.theme_color
    elif line_color.type == MSO_COLOR_TYPE.RGB:
        print("color == %s" % line_color.rgb
    else:
        print("No line color")