簡體   English   中英

Python-pptx - 將圖片插入形狀和動畫

[英]Python-pptx - Inserting a picture into a shape & animations

我正在嘗試使用 python-pptx 制作 python 腳本,但我很難將圖片插入到形狀中。 基本上,我需要它來使圖片透明( https://www.shapechef.com/blog/make-image-transparent-in-powerpoint )。

還有一件事,我想在演示文稿中制作一個按鈕以顯示/隱藏圖片(嘗試在 python 中制作)。 我不知道如何在 python-pptx 中使用動畫,你能幫我嗎?

非常感謝,謝謝。 拉茲

添加我用於制作具有透明度的形狀的代碼(只需在其中插入圖片):

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

使用Aspose.Slides for Python ,您可以輕松地將圖像透明度應用於形狀。 以下代碼示例向您展示了如何執行此操作:

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)

這是一個付費產品,但你可以獲得一個臨時許可證來評估這個庫的所有功能。 我在 Aspose 擔任支持開發人員。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM