使用现有的 pptx Python-Pptx 创建新的 pptx [英] create new pptx using existing pptx Python-Pptx

查看:52
本文介绍了使用现有的 pptx Python-Pptx 创建新的 pptx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

from pptx import Presentationprs1 = 演示文稿()prs = Presentation('old.pptx')title_slide_layout = prs1.slide_layouts[0]对于 prs.slides 中的幻灯片:对于 slide.shapes 中的形状:如果不是 shape.has_text_frame:继续对于 shape.text_frame.paragraphs 中的段落:#print(段落.文本)prs1.slides.add_slide(paragraph.text)prs1.save('new.pptx')

解决方案

您只需打开演示文稿,进行所需的更改,然后使用新名称保存,即可创建演示文稿的修改过的"副本.

>

from pptx import Presentationprs = Presentation('original.pptx')对于 prs.slides 中的幻灯片:对于 slide.shapes 中的形状:如果不是 shape.has_text_frame:继续text_frame = shape.text_frametext_frame.text = 翻译(text_frame.text)prs.save('new.pptx')

如果您提供 translate(text) ->翻译文本 功能,它会做你所要求的.

I am trying to create new.pptx using an old.pptx . old.pptx is having 4 slides in it . I want to create the new.pptx with almost same content with few text modifications in 4 slides . I skipped the modification part from the below code, you can take an example like converting lower cases to upper case..Need to do these things in run time, so that if i just pass the old.pptx it will do the required operation and then write it to new pptx with same no. of slides..I am not sure how to tweak below, may be need to change it completely . Please have a look to below code..

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

解决方案

You can create a "modified" copy of a presentation simply by opening it, making the changes you want, and then saving it with a new name.

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

If you provide a translate(text) -> translated text function to this, it will do what you're asking for.

这篇关于使用现有的 pptx Python-Pptx 创建新的 pptx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆