Find Out whether AutoShape Geometry in Presentation Is Closed or Not in Python

Hello! I am having troubles in finding out whether an AutoShape is closed or not. I attached a sample python file and a sample presentation with 3 shapes, two of which are closed and 1 which isn’t (the arc). My question is, how can I know whether I should close the shape? (I am extracting the shape paths and rendering them using Matplotlib for some use case). For example, the rectangle shape has these points and commands:

Shape name:  Rectangle 13
Commands: ['moveTo', 'lnTo', 'lnTo', 'lnTo']
Points: [[0.0, 0.0], [171.233154296875, 0.0], [171.233154296875, 83.93779754638672], [0.0, 83.93779754638672]]

and if you were to draw this, you would not get a closed shape (the left edge would be missing, which connects the last and first point). Also, when I call the path.close_figure() function (which is commented out in the code), it doesn’t change anything in the geometry paths.

sample.zip (23.4 KB)

Any help would be appreciated, thanks!

@adisa.bolic,
Thank you for posting the question.

Geometric shapes in PowerPoint presentations can consist of several geometric paths, each of which can be either closed or not. To check if a path is closed using Aspose.Slides for Python you can check if the last segment of the path is PathCommandType.CLOSE. The following code example shows you how to do this:

with slides.Presentation("sample.pptx") as presentation:
    first_slide = presentation.slides[0]
    for shape in first_slide.shapes:
        if isinstance(shape, GeometryShape):
            print("Shape name:", shape.name)
            for path_index, path in enumerate(shape.get_geometry_paths()):
                if not path.path_data:
                    continue
                last_segment = path.path_data[-1]
                is_closed = last_segment.path_command == PathCommandType.CLOSE
                print("Path {0} is closed: {1}".format(path_index, is_closed))
            print()

Output:

Shape name: Google Shape;54;p13
Path 0 is closed: True

Shape name: Rectangle 13
Path 0 is closed: True

Shape name: Arc 14
Path 0 is closed: True
Path 1 is closed: False

In your sample presentation, the last shape contains two geometric paths.

Got it, thanks! My issue was that I had the if condition if path_segment.segment_data.length > 0:, while the close command doesn’t contain any data.

One more question tho, as you mentioned a shape can have more geometry paths, but how can I know which one is used for rendering the shape in pptx? For example, in the attached sample.pptx file, the left brace shape has two identical paths, only difference being that the first one is closed, while the second isn’t and obviously the shape itself is not closed when looking in Powerpoint. How can I know which path was used for rendering?

Shape name:  Left Brace 5
Path 1
Commands: ['moveTo', 'arcTo', 'lnTo', 'arcTo', 'arcTo', 'lnTo', 'arcTo', 'close']
Points: [[43.65126037597656, 119.29409790039062], [21.82563018798828, 3.6374595165252686, 90.0, 90.0], [21.82563018798828, 63.284507751464844], [21.82563018798828, 3.6374595165252686, 0.0, -90.0], [21.82563018798828, 3.6374595165252686, 90.0, -90.0], [21.82563018798828, 3.6374595165252686], [21.82563018798828, 3.6374595165252686, 180.0, 90.0], []]

Path 2
Commands: ['moveTo', 'arcTo', 'lnTo', 'arcTo', 'arcTo', 'lnTo', 'arcTo']
Points: [[43.65126037597656, 119.29409790039062], [21.82563018798828, 3.6374595165252686, 90.0, 90.0], [21.82563018798828, 63.284507751464844], [21.82563018798828, 3.6374595165252686, 0.0, -90.0], [21.82563018798828, 3.6374595165252686, 90.0, -90.0], [21.82563018798828, 3.6374595165252686], [21.82563018798828, 3.6374595165252686, 180.0, 90.0]]

sample.pptx.zip (20.9 KB)

@adisa.bolic,
In the case, you should check the stroke property from the GeometryPath class.

Perfect, exactly what I needed. Thanks for the help @andrey.potapov!

@adisa.bolic,
Thank you for your feedback.