Python, how to insert value in Powerpoint template?
<p>I want to use an existing powerpoint presentation to generate a series of reports:</p>

<p>In my imagination the powerpoint slides will have content in such or similar form:</p>

<pre><code>Date of report: {{report_date}} 
Number of Sales: {{no_sales}}
...
</code></pre>

<p>Then my python app opens the powerpoint, fills in the values for this report and saves the report with a new name.
I googled, but could not find a solution for this.</p>

<p>There is python-pptx out there, but this is all about creating a new presentation and not inserting values in a template.</p>

<p>Can anybody advice?</p>

<p>Ultimately, barring some other library which has additional functionality, you need some sort of brute force approach to iterate the Slides collection and each Slide's respective Shapes collection in order to identify the matching shape (unless there is some other library which has additional "Find" functionality in PPT).  Here is brute force using only <code>win32com</code>:</p>

<pre><code>from win32com import client

find_date = r'{{report_date}}'
find_sales = r'{{no_sales}}'
report_date = '01/01/2016' # Modify as needed
no_sales = '604'           # Modify as needed

path = 'c:/path/to/file.pptx'
outpath = 'c:/path/to/output.pptx'
ppt = client.Dispatch("PowerPoint.Application")
pres = ppt.Presentations.Open(path, WithWindow=False)
for sld in pres.Slides:
    for shp in sld.Shapes:
        with shp.TextFrame.TextRange as tr:
            if find_date in tr.Text
                tr.Replace(find_date, report_date)
            elif find_sales in shp.TextFrame.Characters.Text
                tr.Replace(find_sales, no_sales)
pres.SaveAs(outpath)
pres.Close()
ppt.Quit()
</code></pre>

<p>If these strings are inside other strings with <em>mixed</em> text formatting, it gets trickier to preserve existing formatting, but it should still be possible.</p>

<p>If the template file is still in design and subject to your control, I would consider giving the shape a unique identifier like a <code>CustomXMLPart</code> or you could assign something to the shapes' <code>AlternativeText</code> property. The latter is easier to work with because it doesn't require well-formed XML, and also because it's able to be seen &amp; manipulated via the native UI, whereas the <code>CustomXMLPart</code> is only accessible programmatically, and even that is kind of counterintuitive. You'll still need to do shape-by-shape iteration, but you can avoid the string comparisons just by checking the relevant property value.</p>

python powerpoint