首页 > 解决方案 > 使用现有的 pptx Python-Pptx 创建新的 pptx

问题描述

我正在尝试使用 old.pptx 创建 new.pptx 。old.pptx 中有 4 张幻灯片。我想在 4 张幻灯片中创建具有几乎相同内容且文本修改很少的 new.pptx。我从下面的代码中跳过了修改部分,你可以举一个例子,比如将小写字母转换为大写字母。需要在运行时做这些事情,这样如果我只是通过 old.pptx 它将执行所需的操作和然后将其写入具有相同编号的新 pptx。幻灯片..我不知道如何在下面进行调整,可能需要完全改变它。请看下面的代码..

from pptx import Presentation

prs1 = Presentation() 

prs = Presentation('old.pptx') 

title_slide_layout = prs1.slide_layouts[0] 
for slide in prs.slides: 
     for shape in slide.shapes: 
            if not shape.has_text_frame: 
                    continue 
            for paragraph in shape.text_frame.paragraphs: 
                    #print(paragraph.text) 
                    prs1.slides.add_slide(paragraph.text)
     prs1.save('new.pptx')

标签: pythonpython-3.xpowerpointpython-pptx

解决方案


您只需打开演示文稿,进行所需的更改,然后用新名称保存它,即可创建演示文稿的“修改”副本。

from pptx import Presentation

prs = Presentation('original.pptx')
for slide in prs.slides: 
    for shape in slide.shapes: 
        if not shape.has_text_frame: 
            continue
        text_frame = shape.text_frame
        text_frame.text = translate(text_frame.text)

prs.save('new.pptx')

如果您为此提供translate(text) -> translated text功能,它将执行您的要求。


推荐阅读