【问题标题】:Copy slide with images python pptx复制带有图像的幻灯片 python pptx
【发布时间】:2021-03-06 11:46:15
【问题描述】:

我的最终目标是更改演示文稿的主题。为此,我创建了一个源模板和新模板(具有正确的主题)。我遍历源模板中的每张幻灯片,然后使用下面的代码 - source 向新模板中添加一张新幻灯片,其中包含源的内容。如果有更好的方法来做到这一点,我很乐意听到。

这适用于文本和文本框,但是测试图像无法显示在新的 powerpoint 中(如下图所示):

代码

def copy_slide_from_external_prs(self, src, idx, newPrs):

    # specify the slide you want to copy the contents from
    src_slide = src.slides[idx]

    # Define the layout you want to use from your generated pptx
    slide_layout = newPrs.slide_layouts[2]

    # create now slide, to copy contents to
    curr_slide = newPrs.slides.add_slide(slide_layout)


    # remove placeholders
    for p in [s.element for s in curr_slide.shapes if 'Text Placeholder' in s.name or 'Title' in s.name]:
        p.getparent().remove(p)

    # now copy contents from external slide, but do not copy slide properties
    # e.g. slide layouts, etc., because these would produce errors, as diplicate
    # entries might be generated
    for shp in src_slide.shapes:
        el = shp.element
        newel = copy.deepcopy(el)

        curr_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')
    
    return newPrs

我尝试了许多不同的解决方案,并尝试使用源图像中的image.blob 属性创建新图片。但是,图像没有元素。我是否需要将 blob 转换为 PNG,保存它,然后使用保存的 PNG 创建新图像?

必须有更好的方法来做到这一点。再说一次,我只是想换个主题。

提前致谢!

【问题讨论】:

    标签: image python-pptx


    【解决方案1】:

    这是我开发的解决方法。我首先检查形状是否是图像,如果是,我将图像写入本地目录。然后我使用该保存的图像将图片添加到幻灯片中。最后,我删除了本地保存的图片。

    现在这个 copy_slide 函数适用于图像:

    def copy_slide_from_external_prs(src, idx, newPrs):
    
        # specify the slide you want to copy the contents from
        src_slide = src.slides[idx]
    
        # Define the layout you want to use from your generated pptx
        SLD_LAYOUT = 5
        slide_layout = prs.slide_layouts[SLD_LAYOUT]
    
        # create now slide, to copy contents to
        curr_slide = newPrs.slides.add_slide(slide_layout)
    
        # create images dict
        imgDict = {}
    
        # now copy contents from external slide, but do not copy slide properties
        # e.g. slide layouts, etc., because these would produce errors, as diplicate
        # entries might be generated
        for shp in src_slide.shapes:
            if 'Picture' in shp.name:
                # save image
                with open(shp.name+'.jpg', 'wb') as f:
                    f.write(shp.image.blob)
    
                # add image to dict
                imgDict[shp.name+'.jpg'] = [shp.left, shp.top, shp.width, shp.height]
            else:
                # create copy of elem
                el = shp.element
                newel = copy.deepcopy(el)
    
                # add elem to shape tree
                curr_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')
    
        # add pictures
        for k, v in imgDict.items():
            curr_slide.shapes.add_picture(k, v[0], v[1], v[2], v[3])
            os.remove(k)
    

    【讨论】:

    • 很好的解决方案!一个小的改进是生成一个唯一的 ID 用作临时文件名(例如使用 secrets.token_hex(10))。我发现同一张幻灯片上的一些图像具有相同的名称(例如“图片 8”),如果没有这种改编,只有一张图像(具有相同名称)会被复制
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 2016-05-11
    • 1970-01-01
    • 2022-09-30
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多