scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.35k stars 510 forks source link

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

Open LillianJensen opened 2 years ago

LillianJensen commented 2 years ago

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.