Parsa HR
Parsa HR

Reputation: 41

Edit powerpoint .pptx template by python

I am trying to automate report generating in PowerPoint by python. I wanted to know if there is any way to detect an existing textbox from a PowerPoint template and then fill it with some text in python?

Upvotes: 4

Views: 9151

Answers (2)

Andrey Potapov
Andrey Potapov

Reputation: 67

If I understand correctly, your presentation contains placeholders for text filling. The following code example shows you how to fill a footer on the first slide with Aspose.Slides for Python via .NET:

import aspose.slides as slides

with slides.Presentation("example.pptx") as presentation:
    firstSlide = presentation.slides[0]
    for shape in firstSlide.shapes:
        # AutoShape objects have text frames 
        if (isinstance(shape, slides.AutoShape) and shape.placeholder is not None):
            if shape.placeholder.type == slides.PlaceholderType.FOOTER:
                shape.text_frame.text = "My footer text"
    presentation.save("example_out.pptx", slides.export.SaveFormat.PPTX)

I work as a Support Developer at Aspose.

Upvotes: -1

Davinder Singh
Davinder Singh

Reputation: 2162

Main logic is that how to find a placeholder which is given by template by default as well as text-box on non-template-pages. We can take different type to extract data and fill placeholder and text-box like From txt file, form web scraping and many more. Among them we have taken our data list_ object.

1. Lets we n page and we are accessing page 1 so we can access this page usng this code :

(pptx.Presentation(inout_pptx)).slides[0]

2. To select placeholder by default provided in template we will use this code and we will iterator over all placehodler

slide.shapes

3. To update particular placeholder use this :

shape.text_frame.text = data

CODE :

import pptx

inout_pptx = r"C:\\Users\\lenovo\\Desktop\\StackOverFlow\\python_pptx.pptx"
list_data = [
    'Quantam Computing dsfsf ', 
    'Welcome to Quantam Computing Tutorial, hope you will get new thing',
    'User_Name sd',
    '<Enrollment Number>']
"""open file"""
prs = pptx.Presentation(inout_pptx)
"""get to the required slide"""
slide = prs.slides[0]
"""Find required text box"""
for shape, data in zip(slide.shapes, list_data):
    if not shape.has_text_frame:
        continue
    shape.text_frame.text = data

"""save the file"""
prs.save(inout_pptx)

RESULTS : enter image description here

Upvotes: 3

Related Questions