scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.26k stars 498 forks source link

Create multiple slides with concurrent.futures #975

Closed dieissonmartins closed 1 month ago

dieissonmartins commented 1 month ago

Any way to create slides in parallel with concurrent.futures? My intention would be to create super large presentations in seconds

Screenshot from 2024-05-08 15-55-59

ex: ` import concurrent.futures from pptx import Presentation

def create_slide(number): slide = prs.slides.add_slide(prs.slide_layouts[0]) title = slide.shapes.title title.text = f'Slide {number}'

print(f'Creating slide {number}')

def main(): total_slides = 10 with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: futures = [executor.submit(create_slide, i) for i in range(1, total_slides + 1)] for future in concurrent.futures.as_completed(futures): future.result()

prs.save('presentation.pptx')

if name == "main": prs = Presentation() main()`

scanny commented 1 month ago

I doubt it. At least not transparently. python-pptx is not design for concurrency and lxml would have to be concurrent too, which doesn't sound likely.

What I would do personally is profile the code and see what parts are taking the most time and then address those. It could be you could do some "preparation" concurrently, like gather things from databases and so on into memory and then write the PPTX in a single thread but using all those assembled resources.