Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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')
like image 288
Xzog Avatar asked Dec 01 '25 14:12

Xzog


1 Answers

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>
like image 72
Chong Zhou Avatar answered Dec 03 '25 05:12

Chong Zhou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!