Python-pptx – How to insert a slide number for all slides

Question:

I am trying to automatically insert slide numbers to all my slides in PPT that is messy and strictly formatted. When we do manually,we are not able to do it for all the PPTs due to manual work involved. Is there anyway to do this for all 35 plus slides in 20 plus PPT files? When we do using python inbuilt option menus ,it is not working and qe are unable to figure out why. Could be because of our messy format. So, want to use python pptx, insert text box and number them

I referred the SO post and tried the below

i=0
for slide in slides
    i = i + 1
    txBox = slide.shapes.add_textbox(0,0,10,12)
    tf = txBox.text_frame
    tf.text = i
    tf.font.size = (11)

But not sure whether am doing it right. How can I make the slide numbers appear at the bottom of slide?

update based on Luke answer

for slide in presentation.slides:
    i = i + 1
    txBox = slide.shapes.add_textbox(0,0,5,6)
    txBox.height = Inches(0.5)
    txBox.width = Inches(0.5)
    txBox.top = SH - txBox.height
    #if (i % 2) != 0:
    #    txBox.left = SW - Inches(OutsideMargin) - txBox.width
    #else:
    #    
    txBox.right = Inches(float(OutsideMargin)) #updated here
    tf = txBox.text_frame
    tf.vertical_anchor = MSO_ANCHOR.MIDDLE
    p = tf.paragraphs[0]
    p.alignment = PP_ALIGN.RIGHT  #updated here
    run = p.add_run()
    run.text = "Page" + str(i)
    run.font.size = Pt(11)

enter image description here

enter image description here

Asked By: The Great

||

Answers:

UPDATE

If you want the textbox to always be on the right side just do this

txBox.left = presentation.slide_width - txBox.width

You just need to reposition the textbox. maybe something like this…

from pptx import *
from pptx.util import Inches, Pt
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE, PP_ALIGN

presentation = Presentation("input.pptx")
SH = presentation.slide_height
SW = presentation.slide_width

OutsideMargin = 0.5

i=0
for slide in presentation.slides:
    i = i + 1
    txBox = slide.shapes.add_textbox(0,0,10,12)
    txBox.height = Inches(1)
    txBox.width = Inches(2)
    txBox.top = SH - txBox.height
    if (i % 2) != 0:
        txBox.left = SW - Inches(OutsideMargin) - txBox.width
    else:
        txBox.left = Inches(float(OutsideMargin))
    tf = txBox.text_frame
    tf.vertical_anchor = MSO_ANCHOR.MIDDLE
    p = tf.paragraphs[0]
    p.alignment = PP_ALIGN.CENTER
    run = p.add_run()
    run.text = "Page " + str(i)
    run.font.size = Pt(11)

presentation.save("output.pptx")
Answered By: Luke Bowes