【问题标题】:Shape Color in RGBRGB 中的形状颜色
【发布时间】:2020-02-25 16:58:44
【问题描述】:

我正在尝试读取 RGB 中形状的颜色,但是它以十六进制格式出现...

对于 slide_3.shapes 中的形状: 如果 shape.name[:9] == '矩形': 打印(shape.fill.fore_color.rgb, shape.line.color.rgb)

72F91E 000000 72F91E 000000 72F91E 000000 72F91E 000000

【问题讨论】:

  • 似乎是一个类似的问题:See That

标签: python-pptx


【解决方案1】:

shape.fill.fore_color.rgb 是一个RGBColor 对象。

RGBColortuple 的子类型,特别是int 的三元组。 print() 得到的是 str 表示形式,它是两位十六进制数字 R、G 和 B 值的三元组,通常用于在 HTML/CSS 等中指定颜色。

您可以使用以下方法提取红色值:

rgb = shape.fill.fore_color.rgb
red_value = rgb[0]

也许像这样解包元组更容易:

red, green, blue = shape.fill.fore_color.rgb
print("red == %d, green == %d, blue = %d" % (red, green, blue))

或更简单地说:

print("red == %d, green == %d, blue = %d" % shape.fill.fore_color.rgb)

【讨论】:

  • 嗨,Scanny,感谢您的详细解释。这真的很有帮助。
  • @KathiravanSaimoorthy 欢迎来到 StackOverflow!如果它回答了您的问题,您应该接受这个问题(通过单击复选标记)。这让稍后在搜索中找到它的其他人知道这是一个正确的答案。此外,如果您发现它有帮助,您应该使用向上箭头投票。这就是 StackOverflow 经济的运作方式,也是您经常在这里找到有用答案的原因:)
猜你喜欢
  • 2021-02-25
  • 1970-01-01
  • 2017-08-16
  • 2022-11-27
  • 1970-01-01
  • 1970-01-01
  • 2010-09-23
  • 2019-07-02
  • 1970-01-01
相关资源
最近更新 更多