'How do I add a shadow to AutoShapes using Python-pptx?

I just want to add a shadow to the shapes that I am creating while using python-pptx. I have read as many documents about using shadows in python-pptx as I can find but I can not figure out how to actually do it.

I tried shadow = shape.shadow to create a 'ShadowFormat' object but when I try to do shadow.visible I get the error AttributeError: 'ShadowFormat' object has no attribute 'visible'

If anyone could explain how this is done and give an example it would be much appreciated!

Extra info:

This is the page linking to the topic: https://python-pptx.readthedocs.io/en/latest/dev/analysis/shp-shadow.html however there is no example on how to create a shadow for a shape in powerpoint. I have imported the following modules:

from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE
from pptx.enum.action import PP_ACTION
from pptx.util import Cm
from pptx.enum.dml import MSO_THEME_COLOR_INDEX
from pptx.enum.text import MSO_AUTO_SIZE
from pptx.util import Pt

I am using python-pptx v0.6.18 and python v3.8

Edit

Example that creates the shape but no shadow appears:

#Import modules
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE
from pptx.util import Cm
from pptx.enum.dml import MSO_THEME_COLOR_INDEX
from pptx.util import Pt


#Open powerpoint file
prs = Presentation('filename.pptx')

#Create a slide
slidelayout = prs.slide_layouts[0]
slide = prs.slides.add_slide(slidelayout)
shapes = slide.shapes

#Add a shape
shape = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE, Cm(10), Cm(10), Cm(10), Cm(10))

#Create a shadow
shadow = shape.shadow
shadow.inherit = False
shadow.visible = True
shadow.distance = Pt(10)
shadow.shadow_type = 'outer'
shadow.angle = 45
shadow.blur_radius = Pt(5)
shadow.color = MSO_THEME_COLOR_INDEX.ACCENT_5
shadow.transparency = '50'
shadow.distance = Pt(5)
shape.shadow.style = 'outer'

#Save the powerpoint file
prs.save('filename2.pptx')

Example that creates the error message:

#Import modules
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE
from pptx.util import Cm
from pptx.enum.dml import MSO_THEME_COLOR_INDEX
from pptx.util import Pt


#Open powerpoint file
prs = Presentation('filename.pptx')

#Create a slide
slidelayout = prs.slide_layouts[0]
slide = prs.slides.add_slide(slidelayout)
shapes = slide.shapes

#Add a shape
shape = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE, Cm(10), Cm(10), Cm(10), Cm(10))

#Create a shadow
shadow = shape.shadow
shadow.visible


#Save the powerpoint file
prs.save('filename2.pptx')


Solution 1:[1]

The feature <ShadowFormat.visible - applies a reasonable standard shadow override.> is currently out of the scope of pptx.

The command <shadow.inherit = False> is used to remove the default setting with the shadow. By default, the shadow visibility is set to true. If you want to show the shadow, you can either:

  1. set <shadow.inherit = True>
  2. remove <shadow.inherit = False>

Solution 2:[2]

You can use Aspose.Slides for Python to manipulate the shapes. This is a paid library, but you can get a temporary license to evaluate it. The following code example shows you how to add a shape with a shadow to a presentation:

import aspose.slides as slides

# Create a new presentation.
with slides.Presentation() as presentation:
 
    # Create a shape.
    shape = presentation.slides[0].shapes.add_auto_shape(slides.ShapeType.ROUND_CORNER_RECTANGLE, 10, 10, 20, 20)

    # Set a shadow for the shape.
    shape.effect_format.enable_outer_shadow_effect()
    shape.effect_format.outer_shadow_effect.distance = 10
    shape.effect_format.outer_shadow_effect.direction = 45
    shape.effect_format.outer_shadow_effect.blur_radius = 5
    shape.effect_format.outer_shadow_effect.shadow_color.color = presentation.master_theme.color_scheme.accent5.color

    # Save the presentation.
    presentation.save("example.pptx", slides.export.SaveFormat.PPTX)

The result:

enter image description here

Alternatively, you can use Aspose.Slides Cloud SDK for Python. This product provides a REST-based API for presentation processing. It is also a paid product, but you can make 150 free API calls per month for experimentation, learning, and any other purpose. The following code example creates the same shape with the shadow using Aspose.Slides Cloud:

import asposeslidescloud

from asposeslidescloud.apis.slides_api import SlidesApi
from asposeslidescloud.models.shape import Shape
from asposeslidescloud.models.effect_format import EffectFormat
from asposeslidescloud.models.outer_shadow_effect import OuterShadowEffect

slides_api = SlidesApi(None, "my_client_id", "my_client_secret")

# Let's a presentation exists in a storage.
file_name = "example.pptx"
slide_index = 1

color_scheme = slides_api.get_color_scheme(file_name, slide_index)

# Prepare DTO for a shape with the shadow.
shape = Shape()
shape.shape_type = "RoundCornerRectangle"
shape.x = 10
shape.y = 10
shape.width = 20
shape.height = 20
shape.effect_format = EffectFormat()
shape.effect_format.outer_shadow = OuterShadowEffect()
shape.effect_format.outer_shadow.distance = 10
shape.effect_format.outer_shadow.direction = 45
shape.effect_format.outer_shadow.blur_radius = 5
shape.effect_format.outer_shadow.shadow_color = color_scheme.accent5

# Create the shape.
slides_api.create_shape(file_name, slide_index, shape)

I work as a Support Developer at Aspose.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abhishek Dutt
Solution 2 Andrey Potapov