-1

I know how we can add a auto_shape of a certain type, ISOSCELES_TRIANGLE for example. But how can we find the triangles in the shapes of a slide? Is there a way to access MsoAutoShapeType and get the type. I want to count how many triangles there are in a slide. Thanks

Rami.K
  • 189
  • 2
  • 11

1 Answers1

1

Refer to the shape's .auto_shape_type property, which returns one of the MsoAutoShapeType values.

from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE

pres = Presentation("shapes.pptx")
slide = pres.slides[0]
triangles = []
for s in slide.shapes:
    try:
        if s.auto_shape_type == MSO_SHAPE.ISOSCELES_TRIANGLE:
            triangles.append(s)
    except AttributeError as e:
        # shape is **NOT** an auto_shape; ignore
        print(f'{s.name} is not an AutoShape')
        pass

print(len(triangles))
David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • this is only working for rectangles for some reason and not to triangles and ovals, any idea why? Thanks !! – Rami.K Jul 01 '19 at 18:25
  • This works exactly as I expect it to work when I create a presentation with some various auto shapes on it. You will need to do some additional debugging to examine the contents of the shapes which are not included as you expect. Perhaps they are not autoshapes but drawing shapes or something else? It is not possible for me to say without knowing about your file contents. – David Zemens Jul 01 '19 at 18:42
  • 1
    All set thanks! it was a connector problem... I am now checking whether it is an autoshape before doing any operations on them including auto_shape_type. Thanks again ! – Rami.K Jul 02 '19 at 00:36
  • Yep, I discovered you have to check whether it's an autoshape, because other shapes don't have the `auto_shape_type` property, hence my revised answer with error trap :) Cheers – David Zemens Jul 02 '19 at 01:52