'Python-pptx - Inserting a picture into a shape & animations

I'm trying to make a python script using python-pptx but I struggle with inserting a picture into a shape. Basically, I need it for making the picture transparent (https://www.shapechef.com/blog/make-image-transparent-in-powerpoint).

One more thing, I'd like to make a button into the presentation for showing/hiding the picture (trying to make it in python). I ain't sure how to use animations in python-pptx, can you help me with this?

Much appreciated, thank you. Raz

Adding the code I have for making a shape with transparency (just need to insert a picture into it):

from pptx import Presentation
from pptx.oxml.xmlchemy import OxmlElement
from pptx.util import Cm
from pptx.enum.shapes import MSO_SHAPE
from pptx.dml.color import RGBColor

def SubElement(parent, tagname, **kwargs):
        element = OxmlElement(tagname)
        element.attrib.update(kwargs)
        parent.append(element)
        return element

def _set_shape_transparency(shape, alpha):
    """ Set the transparency (alpha) of a shape"""
    ts = shape.fill._xPr.solidFill
    sF = ts.get_or_change_to_srgbClr()
    sE = SubElement(sF, 'a:alpha', val=str(alpha))

## Create presentation
prs = Presentation()
## Add a slide (empty slide layout)
slide = prs.slides.add_slide(prs.slide_layouts[6])
##Add a blue box to the slide
# MSO_FILL_TYPE = MSO_FILL.PICTURE
blueBox = slide.shapes.add_shape(autoshape_type_id=MSO_SHAPE.RECTANGLE,
                         left=Cm(0),
                         top=Cm(0.54),
                         height=Cm(17.97),
                         width=Cm(25.4))
## Make the box blue
blueBoxFill = blueBox.fill
blueBoxFill.solid()
blueBoxFillColour = blueBoxFill.fore_color
blueBoxFillColour.rgb = RGBColor(0,176,240)
## Set the transparency of the blue box to 56%
_set_shape_transparency(blueBox,44000)
## Save the presentation
prs.save('test.pptx')


Solution 1:[1]

If you use Aspose.Slides for Python, you can easily apply image transparency to shapes. The following code example shows you how to do this:

import aspose.slides as slides

# Create a new presentation.
with slides.Presentation() as presentation:
 
    # Add an image to resources of the presentation.
    with open("dog.png", "rb") as image_stream:
        image = presentation.images.add_image(image_stream)

    # Add a rectangle shape.
    rectangle = presentation.slides[0].shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 50, 50, 200, 200)
    
    # Use the picture fill for the shape.
    rectangle.fill_format.fill_type = slides.FillType.PICTURE
    rectangle.fill_format.picture_fill_format.picture.image = image

    # Set the image transparency to 50%.
    rectangle.fill_format.picture_fill_format.picture.image_transform.add_alpha_modulate_fixed_effect(50)

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

This is a paid product, but you can get a temporary license to evaluate the features of this library. 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 Andrey Potapov