Resize Shapes on Presentation Slides in Python

Overview

When you change a presentation’s slide size, you may also need to resize and reposition its shapes to fit the new dimensions. This article shows how to scale shapes manually and how to handle tables on slides, masters, and layouts.

Resize Shapes

To preserve each shape’s relative position and size, multiply its horizontal coordinates and width by the ratio of the new slide width to the original width. Apply the corresponding height ratio to its vertical coordinates and height.

The first example loads a presentation whose slides contain no tables, changes the slide size to A4, and saves the result. It uses SlideSize.set_size with SlideSizeScaleType.DO_NOT_SCALE so that the subsequent loop controls the scaling. The ratios are unitless; shape positions and dimensions are measured in points. Different width and height ratios can distort shapes, and changing a shape’s dimensions does not scale its font sizes.

import aspose.slides as slides

with slides.Presentation("sample.pptx") as presentation:
    # Calculate independent horizontal and vertical scale factors.
    original_size = presentation.slide_size.size
    presentation.slide_size.set_size(slides.SlideSizeType.A4_PAPER, slides.SlideSizeScaleType.DO_NOT_SCALE)
    target_size = presentation.slide_size.size
    width_ratio = target_size.width / original_size.width
    height_ratio = target_size.height / original_size.height

    for slide in presentation.slides:
        for shape in slide.shapes:
            shape.width *= width_ratio
            shape.height *= height_ratio
            shape.x *= width_ratio
            shape.y *= height_ratio

    presentation.save("output.pptx", slides.export.SaveFormat.PPTX)

The second example supports tables and processes shapes on masters and layouts as well as individual slides before saving the resized presentation. Its helper handles each Table by scaling Column.width and Row.minimal_height. It then scales the table’s position without separately scaling its overall dimensions.

import aspose.slides as slides


def resize_shapes(shapes, width_ratio, height_ratio):
    for shape in shapes:
        if isinstance(shape, slides.Table):
            # Table dimensions are controlled by column widths and row heights.
            for column in shape.columns:
                column.width *= width_ratio

            for row in shape.rows:
                row.minimal_height *= height_ratio
        else:
            shape.width *= width_ratio
            shape.height *= height_ratio

        shape.x *= width_ratio
        shape.y *= height_ratio


with slides.Presentation("sample.pptx") as presentation:
    # Calculate independent horizontal and vertical scale factors.
    original_size = presentation.slide_size.size
    presentation.slide_size.set_size(slides.SlideSizeType.A4_PAPER, slides.SlideSizeScaleType.DO_NOT_SCALE)
    target_size = presentation.slide_size.size
    width_ratio = target_size.width / original_size.width
    height_ratio = target_size.height / original_size.height

    # Resize shapes defined on masters and layouts as well as individual slides.
    for master in presentation.masters:
        resize_shapes(master.shapes, width_ratio, height_ratio)

        for layout_slide in master.layout_slides:
            resize_shapes(layout_slide.shapes, width_ratio, height_ratio)

    for slide in presentation.slides:
        resize_shapes(slide.shapes, width_ratio, height_ratio)

    presentation.save("output.pptx", slides.export.SaveFormat.PPTX)

Before and after A4 resizing, the shape and table move and resize. Column widths and minimum row heights increase, while font sizes stay fixed.

In this table-aware resize, the width ratio scales X, shape width and column widths. The height ratio scales Y, shape height and minimum row heights. Font sizes stay fixed.

FAQ

Why are shapes distorted or cut off after resizing a slide?

If you change the slide size without scaling the shapes, they keep their original positions and dimensions and may extend beyond the slide. Scaling width and height by different ratios can also distort shapes.

Does the provided code work for all shape types?

The first example changes shape bounds directly and is intended for slides without tables. The second example handles tables separately. Check the resulting layout, particularly text and tables, because these examples do not scale font sizes and table content can constrain row heights.

How do I resize tables when resizing a slide?

Scale column widths by the width ratio and minimum row heights by the height ratio, then scale the table’s position, as shown in the second example.

Will this resizing work for master slides and layout slides?

Yes. The second example iterates through Presentation.masters and each master’s MasterSlide.layout_slides and applies the same resizing logic to their shapes.

Can I change the orientation of a slide (portrait/landscape) along with the resizing?

Yes. Set SlideSize.orientation after changing the slide size and before reading the target dimensions, so the scale factors reflect the final orientation.

Is there a limit to the slide size I can set?

You can specify custom slide dimensions with SlideSize.set_size. The supported dimensions also depend on the presentation format and the application used to open the file.

How can I prevent shapes with a fixed aspect ratio from becoming distorted?

Use the same scale factor for width and height. To fit the original layout within the new slide dimensions, use the smaller of the width and height ratios for both dimensions and positions. This preserves proportions but may leave unused space along one edge.