Joe Joe - 1 year ago 85
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:

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

and the exception is:

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


Answer Source

You need to call:


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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download