问题
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')
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/52448755/create-new-pptx-using-existing-pptx-python-pptx