scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.4k stars 519 forks source link

radius of ROUNDED_RECTANGLE shape #903

Closed arifroni closed 1 year ago

arifroni commented 1 year ago

Hello

I am creating a rounded rectangle by following the code. Is there any way to change the radius of the shape?

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

pr = Presentation(os.path.join(os.getcwd(),pptx_figure))
slide_template = pr.slide_layouts[3]
slide = pr.slides.add_slide(slide_template)

left = Inches((13.33-(0.33*2))/2+0.33)
top = Inches(3.84)
width = Inches((13.33-(0.33*2))/2)
shape = slide.shapes.add_shape(
    MSO_SHAPE.ROUNDED_RECTANGLE,
    left=left,
    top=top,
    width=width,
    height=width*(10/24)
)

pr.save(pptx_figure)
MartinPacker commented 1 year ago

In md2pptx I have the following code:

            if cardShape == "rounded":
                # Rounded Rectangle for card
                cardBackgroundShape = slide.shapes.add_shape(
                    MSO_SHAPE.ROUNDED_RECTANGLE,
                    Inches(0),
                    Inches(0),
                    Inches(0),
                    Inches(0),
                )

                # Rounding adjustment works better with different values for horizontal and vertical cards
                if cardLayout == "horizontal":
                    # Make the rounding radius small. This is 1/4 the default
                    cardBackgroundShape.adjustments[0] = 0.0416675
                else:
                    # Make the rounding radius smallish. This is 1/2 the default
                    cardBackgroundShape.adjustments[0] = 0.083335

which does indeed get me a rounded rectangle with a specific radius. The operative line is

                    cardBackgroundShape.adjustments[0] = 0.0416675

I would play with the actual value - to get one you want.

(Note: My purpose isn't to "sell" you md2pptx; Rather to share the relevant piece of code.)

arifroni commented 1 year ago

In md2pptx I have the following code:

            if cardShape == "rounded":
                # Rounded Rectangle for card
                cardBackgroundShape = slide.shapes.add_shape(
                    MSO_SHAPE.ROUNDED_RECTANGLE,
                    Inches(0),
                    Inches(0),
                    Inches(0),
                    Inches(0),
                )

                # Rounding adjustment works better with different values for horizontal and vertical cards
                if cardLayout == "horizontal":
                    # Make the rounding radius small. This is 1/4 the default
                    cardBackgroundShape.adjustments[0] = 0.0416675
                else:
                    # Make the rounding radius smallish. This is 1/2 the default
                    cardBackgroundShape.adjustments[0] = 0.083335

which does indeed get me a rounded rectangle with a specific radius. The operative line is

                    cardBackgroundShape.adjustments[0] = 0.0416675

I would play with the actual value - to get one you want.

(Note: My purpose isn't to "sell" you md2pptx; Rather to share the relevant piece of code.)

works exactly like I wanted. :)