python-pptx icon indicating copy to clipboard operation
python-pptx copied to clipboard

Added a:fld type to paragraphs for page numbers and datetimes

Open LillianJensen opened this issue 3 years ago • 1 comments

LillianJensen avatar Mar 18 '22 17:03 LillianJensen

I probably need to clean up a bit before this is ready to merge into the original repository (any feedback would be appreciated, the code is very dense, and there is a lot of indirection happening). Currently I have tested it and it does work with the following code:

import pptx

ppt = pptx.Presentation()
ppt_slide = ppt.slides.add_slide(ppt.slide_layouts[6]) # blank Slide
t = ppt_slide.shapes.add_textbox(0, 0, 100, 100)
p = t.text_frame.paragraphs[0]

r = p.add_run()
r.text = "Test"

p = t.text_frame.add_paragraph()
f = p.add_field()
f.text = "1"
f.type = "slidenum"

with open("test.pptx", 'wb') as f:
    ppt.save(f)

The field properly updates when slides are added, or moved.

LillianJensen avatar Mar 18 '22 17:03 LillianJensen