5

I am generating new slides using python pptx and I see no way to update the slide number. I have set the footer in master layout but when I try to read the shapes, I don't see that component over there.

My code:

from pptx import Presentation

prs = Presentation('sample_ppt.pptx')
title_slide_layout = prs.slide_layouts[14]
slide = prs.slides.add_slide(title_slide_layout)

for shape in slide.shapes:
    print(shape.name)
prs.save('hh.pptx')
wayfare
  • 1,802
  • 5
  • 20
  • 37

1 Answers1

3

This is a common confusion, possibly because the way PowerPoint handles footers is, well, confusing :)

The short answer is that you need to put a plain-textbox shape (not a footer placeholder) on the master slide, and insert into that textbox a slide-number field, using Insert > Slide Number from the menu. On the master, it will appear something like <#>, but on the slide that inherits from that master, it will appear as the slide number.

scanny
  • 26,423
  • 5
  • 54
  • 80
  • So if I wanna do this with python-pptx, I need to create text-holder and insert page number by myself? – zx1986 Dec 10 '18 at 02:59
  • 1
    Adding fields (like the slide-number field) isn't supported yet by the API in `python-pptx`. Most folks add that to the master of their starting template. If you need to retroactively add this to a set of existing presentations you'd need to pursue modifying the XML of a text-box paragraph. It looks like the `a:t` element is wrapped with an `a:fld` element with some simple attributes. – scanny Dec 10 '18 at 22:55
  • Thanks! @scanny, I add a textbox to the corner of a slide, then put the index number into it :-) – zx1986 Dec 12 '18 at 01:48