4

I'm using the pywin32.client extension for python and building a Word document. I have tried a pretty good host of methods to generate a ToC but all have failed.

I think what I want to do is call the ActiveDocument object and create one with something like this example from the MSDN page:

Set myRange = ActiveDocument.Range(Start:=0, End:=0) 
ActiveDocument.TablesOfContents.Add Range:=myRange, _ 
 UseFields:=False, UseHeadingStyles:=True, _ 
 LowerHeadingLevel:=3, _ 
 UpperHeadingLevel:=1

Except in Python it would be something like:

wordObject.ActiveDocument.TableOfContents.Add(Range=???,UseFiles=False, UseHeadingStyles=True, LowerHeadingLevel=3, UpperHeadingLevel=1)

I've built everything so far using the 'Selection' object (example below) and wish to add this ToC after the first page break.

Here's a sample of what the document looks like:

objWord = win32com.client.Dispatch("Word.Application")
objDoc = objWord.Documents.Open('pathtotemplate.docx') #
objSel = objWord.Selection
#These seem to work but I don't know why...
objWord.ActiveDocument.Sections(1).Footers(1).PageNumbers.Add(1,True)
objWord.ActiveDocument.Sections(1).Footers(1).PageNumbers.NumberStyle = 57
objSel.Style = objWord.ActiveDocument.Styles("Heading 1")
objSel.TypeText("TITLE PAGE AND STUFF")
objSel.InsertParagraph()
objSel.TypeText("Some data or another"
objSel.TypeParagraph()
objWord.Selection.InsertBreak()
####INSERT TOC HERE####

Any help would be greatly appreciated! In a perfect world I'd use the default first option which is available from the Word GUI but that seems to point to a file and be harder to access (something about templates).

Thanks

Abraxas
  • 341
  • 1
  • 9
  • 28
  • As toc is automatically constructed from yhe header structure, why not just insert a toc in a template and create a doc from that template? – DisappointedByUnaccountableMod Mar 15 '17 at 22:11
  • @barny How could I make it stick to the second page? As I'm using the `WordObject.Selection.action()` structure the document just kinda forms as I write it. If I put a ToC in the template it will just be pushed to the bottom right? Is there a way I can specify the selection start on the top of page one, then jump to after the ToC? – Abraxas Mar 15 '17 at 22:19
  • 1
    Put a bookmark/marker in your template, on page 3 (or wherever). Then in your code search for the marker and that's where you add content. You can then put any other formatting you want in the template, header/footer, styles, graphics, etc. With absolutely no code. This way when someone says "I want the Normal font bigger" all you have to do is edit the template, no need to change a line of code. You are using styles for most formatting and not direct specific (handcoded) formatting, except in special cases, right? And then your customer is very happy. – DisappointedByUnaccountableMod Mar 15 '17 at 22:33
  • I'll look in to trying this tomorrow and let you know how it goes. Thank you much! – Abraxas Mar 16 '17 at 01:10

1 Answers1

3

Manually, edit your template in Word, add the ToC (which will be empty initially) any intro stuff, header/footers etc., then at where you want your text content inserted (i.e. after the ToC) put a uniquely named bookmark. Then in your code, create a new document based on the template (or open the template then save it to a different name), search for the bookmark and insert your content there. Save to a different filename.

This approach has all sorts of advantages - you can format your template in Word rather than by writing all the code details, and so you can very easily edit your template to update styles when someone says they want the Normal font to be bigger/smaller/pink you can do it just by editing the template. Make sure to use styles in your code and only apply formatting when it is specifically different from the default style.

Not sure how you make sure the ToC is actually generated, might be automatically updated on every save.