Joe Joe - 1 month ago 14
Python Question

In which way are these two PowerPoint Shapes different (accessed via API)

As part of an open source assistive technology project (here), I am accessing a PowerPoint file by python API (python-pptx).

Some shapes are causing exceptions when I look at color. But it's hard to see which.

I've created this minimal example: https://www.dropbox.com/s/o5z22lqwb66jemq/forUpload.pptx?dl=0

and indeed, here's a screenshot:

enter image description here

It consists of a single slide, with shapes that work on the left, and shapes that fail on the right. Using the format painter will make a shape work (or not, depending on the source of the formatting) but I've been through every way of checking the formats that I can think of, and both sets of shapes appear identical.

How are the two sets of shapes in the minimal example different?

(for those who might be interested from a API point of view. The line that causes the problem is:

self.colours[co][ro] = shape.fill.fore_colour.rgb


which is in https://github.com/joereddington/TheOpenVoiceFactory/blob/master/grab_text.py

and the exception is:

Traceback (most recent call last): File "grab_text.py", line 374, in <module>
grids = extract_grid(prs) File "grab_text.py", line 353, in extract_grid
grids.append(Grid(prs, slide, gridSize)) File "grab_text.py", line 119, in __init__
self.process_shape(shape) File "grab_text.py", line 164, in process_shape
ro] = shape.fill.fore_color.rgb File "/Library/Python/2.7/site-packages/pptx/dml/fill.py", line 43, in fore_color
return self._fill.fore_color File "/Library/Python/2.7/site-packages/pptx/dml/fill.py", line 161, in fore_color
raise TypeError(tmpl) TypeError: a transparent (background) fill has no foreground color


)

Answer

You need to call:

shape.fill.solid()

before attempting to access the fore_color attribute.

A fill can have several types, each of which have a different attribute set. The .fore_color attribute is particular to a solid fill. By default, the fill is transparent (i.e. None).

A little more about it in the documentation here: http://python-pptx.readthedocs.io/en/latest/user/autoshapes.html#fill