Skip to content Skip to sidebar Skip to footer

How Can Dynamic Page Numbers Be Inserted Into Added Slides?

When adding slides via a modified python-pptx, placeholders appear for the slide number on each slide. Instead of the actual slide number, however, the words 'Slide Number' appears

Solution 1:

An "auto-update" slide-number in PowerPoint is a field. The XML looks like this:

<a:fldid="{1F4E2DE4-8ADA-4D4E-9951-90A1D26586E7}"type="slidenum"><a:rPrlang="en-US"smtClean="0"/><a:t>2</a:t></a:fld>

So the short answer to your question is no. There is no string you can insert in a textbox that triggers this XML to be inserted and there is no API support for adding fields yet either.

The approach would be to get a <a:p> element for a textbox and insert this XML into it using lxml calls (a process commonly referred to as a "workaround function" in other python-pptx posts).

You can get the a:p element like so:

p = textbox.text_frame.paragraphs[0]._p

Something like this might work, and at least provides a sketch of that approach:

from pptx.oxml import parse_xml
from pptx.oxml.ns import nsdecls

# ---get a textbox paragraph element---
p = your_textbox.text_frame.paragraphs[0]._p

# ---add fld element---
fld_xml = (
    '<a:fld %s id="{1F4E2DE4-8ADA-4D4E-9951-90A1D26586E7}" type="slidenum">\n''  <a:rPr lang="en-US" smtClean="0"/>\n''  <a:t>2</a:t>\n''</a:fld>\n' % nsdecls("a")
)
fld = parse_xml(fld_xml)
p.append(fld)

The number enclosed in the a:t element is the cached slide number and may not be automatically updated by PowerPoint when opening the presentation. If that's the case, you'll need to get it right when inserting, but it will be automatically updated when you shuffle slides.

Post a Comment for "How Can Dynamic Page Numbers Be Inserted Into Added Slides?"