I'm working with pptx to create templates based on info in existing slides. I can create a slide, define its layout, enter text, etc. I can do all the basics. I want to create a slide in a new ppt based on an input ppt and use the same amount of slides as the original, all with their original layouts, and just change some text. How can I access a specific slide's layout # without knowing beforehand? All the other answers say "new_slide_layout = prs.slides.slide_layouts[i]" where i is a number they have defined in the code (usually 0). I'm trying to get that i from the input file and use it without choosing slide type as a user. How can I do this? Latest functional code:
inputf = pptx.Presentation(input_file)
prs = pptx.Presentation()
old_slide_list = []
slides = inputf.slides
slide_count = 0
for slide in slides:
old_slide_list.append(slide)
slide_count += 1
old_text_runs = []
new_text_runs = []
for i in range(0, slide_count):
old_slide = old_slide_list[i]
old_runs_count = 0
nl = int(input("Enter your choice for this slide's layout (0-8):"))
new_layout = prs.slide_layouts[nl]
new_slide = prs.slides.add_slide(new_layout)
I can allow the user to choose their own slide layout but if I try to rip it directly from the old slide and use it for the new slide it damages the new ppt and it will ask you to repair each time you open it. Older code that damages the presentation:
import copy
for slide in slides:
print('slide number %d' % slides.index(slide))
count += 1
tempct = 0
prs2 = pptx.Presentation()
while tempct != count:
template = prs.slides[tempct]
template_layout = template.slide_layout
new_slide = prs2.slides.add_slide(template_layout)
for shp in template.shapes:
el = shp.element
newel = copy.deepcopy(el)
new_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')
tempct += 1
prs2.save(output_file)