2

Is there a way to add page numbers to the lower right corner of a Word document using Python win32com? I am able to add headers and footers, but I can't find a way to add page numbers in the format PageNumber of TotalPages (for example: 1 of 5)

Below is the code to add centered headers and footers to a page

from win32com.client import Dispatch as MakeDoc
filename = name + '.doc'
WordDoc = MakeDoc("Word.Application")
WordDoc = WordDoc.Documents.Add()
WordDoc.Sections(1).Headers(1).Range.Text = name
WordDoc.Sections(1).Headers(1).Range.ParagraphFormat.Alignment = 1
WordDoc.Sections(1).Footers(1).Range.Text = filename
WordDoc.Sections(1).Footers(1).Range.ParagraphFormat.Alignment = 1

Thanks

7
  • 1
    Please post your solution as an answer to this one so that others with the same issue (i.e. me) can figure it out as well Commented Jun 11, 2014 at 16:14
  • To insert page numbers use the following statements: WordDoc.Sections(1).Footers(1).PageNumbers.Add(2,True) WordDoc.Sections(1).Footers(1).PageNumbers.NumberStyle = 57 However, the format of the number is -PageNumber- instead of the one I was looking for, but I'm fine with it anyways Commented Jun 11, 2014 at 17:42
  • Thanks, can you post and documentation for that? Commented Jun 11, 2014 at 19:07
  • 1
    Sure, the documentation for the adding the page numbers is here and the one for the number style is here. Enjoy Commented Jun 11, 2014 at 20:08
  • You should post an answer not a comment; as it is your question will appear open (unanswered) in SO search lists. Commented Jun 12, 2014 at 3:00

2 Answers 2

5

To insert page numbers use the following statements:

WordDoc.Sections(1).Footers(1).PageNumbers.Add(2,True)
WordDoc.Sections(1).Footers(1).PageNumbers.NumberStyle = 57

However, the format of the page number is -page number-. Documentation for inserting the page number is here, and the one for the number style is here

Sign up to request clarification or add additional context in comments.

Comments

2

I know this is an old question but I was banging my head against a wall trying to figure out the same thing and ended up working out a solution that's rather ugly, but gets the job done. Note that I had to redefine activefooter after inserting wdFieldPage or else the resulting footer would look like of 12 rather than 1 of 2.

The answer to this vba question was helpful when I was trying to figure out the formatting.

I'm using Python 3.4, testdocument.doc is just an existing .doc file with some random text spread across two pages and no existing footer.

w = win32com.client.gencache.EnsureDispatch("Word.Application")          
w.Visible = 0
adoc = w.Documents.Open("C:\\temp1\\testdocument.doc")

activefooter = adoc.Sections(1).Footers(win32com.client.constants.wdHeaderFooterPrimary).Range
activefooter.ParagraphFormat.Alignment = win32com.client.constants.wdAlignParagraphRight
activefooter.Collapse(0)
activefooter.Fields.Add(activefooter,win32com.client.constants.wdFieldPage)
activefooter = adoc.Sections(1).Footers(win32com.client.constants.wdHeaderFooterPrimary).Range 
activefooter.Collapse(0)
activefooter.InsertAfter(Text = ' of ')
activefooter.Collapse(0)
activefooter.Fields.Add(activefooter,win32com.client.constants.wdFieldNumPages)  
adoc.Save()
adoc.Close()
w.Quit()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.