3

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')
RonyA
  • 585
  • 3
  • 11
  • 26

2 Answers2

2

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.

scanny
  • 26,423
  • 5
  • 54
  • 80
  • So you are saying, no need to iterate through each slide , just open the slide and make changes and then save it ? – RonyA Sep 22 '18 at 04:19
  • I have to translate each slide of ppt in some different language , translator only accept text , thats why i want to iterate through texts of slides and then add it in new pptx and slides sequently . Directly passing ppptx will not work .. – RonyA Sep 22 '18 at 04:29
  • I've elaborated my code sample. It's best to describe your use case well in the original question so folks can understand your objectives; this leads to the best possible answers. Something to keep in mind for next time. – scanny Sep 22 '18 at 05:04
  • Its working . However if there is some text starting with "bullet" or others the "bullet" is missing but yes I get those text .. – RonyA Sep 23 '18 at 04:28
  • A bullet is not part of the text. Rather it's an attribute of the paragraph. – scanny Sep 23 '18 at 18:34
  • How all can be clubbed so nothing get missed ? – RonyA Sep 23 '18 at 18:35
  • 1
    @RonyA dealing with bullets is a separate question and one that's been asked and answered a few times already. Why don't you have a search on those and ask a new question if the existing ones don't address you particular case. – scanny Sep 24 '18 at 17:25
  • So this approach works fine to replace text, it seems to completely messes up all formatting (font-type and font-size) at the same time. If you have a similar issue, have a look here (this solved everything for me) https://stackoverflow.com/questions/37924808/python-pptx-power-point-find-and-replace-text-ctrl-h – and0r Jan 24 '22 at 16:18
1

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.

msi_gerva
  • 2,021
  • 3
  • 22
  • 28
  • Before copying the context to new pots slides I have to do some operations on the text extracted from old pptx and then create the new pptx with same no. Of slides .. take an example , I need to edit some text in each slide .. – RonyA Sep 21 '18 at 21:49
  • Anyhow, My main issue is how to create new pptx slide and then copy the content from old pptx creating the same no. of slides... – RonyA Sep 21 '18 at 21:56