简体   繁体   English

Python-pptx - 将图片插入形状和动画

[英]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.我正在尝试使用 python-pptx 制作 python 脚本,但我很难将图片插入到形状中。 Basically, I need it for making the picture transparent ( https://www.shapechef.com/blog/make-image-transparent-in-powerpoint ).基本上,我需要它来使图片透明( 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).还有一件事,我想在演示文稿中制作一个按钮以显示/隐藏图片(尝试在 python 中制作)。 I ain't sure how to use animations in python-pptx, can you help me with this?我不知道如何在 python-pptx 中使用动画,你能帮我吗?

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')

With Aspose.Slides for Python , you can easily apply image transparency to shapes.使用Aspose.Slides for Python ,您可以轻松地将图像透明度应用于形状。 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 all features of this library.这是一个付费产品,但你可以获得一个临时许可证来评估这个库的所有功能。 I work as a Support Developer at Aspose.我在 Aspose 担任支持开发人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM