简体   繁体   English

使用现有的pptx Python-Pptx创建新的pptx

[英]create new pptx using existing pptx Python-Pptx

I am trying to create new.pptx using an old.pptx . 我正在尝试使用old.pptx创建new.pptx。 old.pptx is having 4 slides in it . old.pptx里面有4张幻灯片。 I want to create the new.pptx with almost same content with few text modifications in 4 slides . 我想在4张幻灯片中创建几乎相同内容的new.pptx,几乎没有文本修改。 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. 我从下面的代码中跳过了修改部分,你可以举一个例子,比如将小写字母转换成大写字母..需要在运行时执行这些操作,这样如果我只是通过old.pptx,它将执行所需的操作,然后用相同的no将它写入新的pptx。 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. 如果您为此提供translate(text) -> translated text功能,它将执行您要求的操作。

Is one option to make a copy of "old.pptx" with new name and then open the copy for editing? 是否可以选择使用新名称复制“old.pptx”,然后打开副本进行编辑? If that is an option, you could do: 如果这是一个选项,你可以这样做:

import subprocess
old_file = 'old.pptx';
new_file = 'new.pptx';
subprocess.call('cp '+old_file+' '+new_file,shell=True)

Now you can open the new file for editing/modification. 现在您可以打开新文件进行编辑/修改。

If you are using Windows machine, you should look for equivalence for cp command. 如果您使用的是Windows机器,则应该查找cp命令的等效性。

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

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