简体   繁体   中英

create new pptx using existing pptx Python-Pptx

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.

Is one option to make a copy of "old.pptx" with new name and then open the copy for editing? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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