1

I am trying to read color of a shape in RGB however, it is coming in hex format...

for shape in slide_3.shapes: if shape.name[:9] == 'Rectangle': print(shape.fill.fore_color.rgb, shape.line.color.rgb)

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

  • Seems to be one similar question: [See That](https://stackoverflow.com/questions/29643352/converting-hex-to-rgb-value-in-python) – Clément Oct 30 '19 at 11:50

1 Answers1

1

The value shape.fill.fore_color.rgb is an RGBColor object.

RGBColor is a subtype of tuple and in particular a 3-tuple of int. What you're getting with print() is the str representation which is a triple of two-hex-digit R, G, and B values, commonly used for specifying colors in for instance HTML/CSS.

You can extract the red value with:

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

Maybe easier is to unpack the tuple like this:

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

or more simply:

print("red == %d, green == %d, blue = %d" % shape.fill.fore_color.rgb)
scanny
  • 26,423
  • 5
  • 54
  • 80
  • Hi Scanny, Thank you for the detailed explanation. This really helped. – Kathiravan Saimoorthy Oct 31 '19 at 03:43
  • @KathiravanSaimoorthy Welcome to StackOverflow! You should accept this question (by clicking the checkmark) if it answered your question. This lets others who find it later on search know it is a correct answer. Also, you should vote it up with the up-arrow if you found it helpful. This is how the StackOverflow economy works and why you so often find useful answers here :) – scanny Oct 31 '19 at 16:12