Python: Create a "Table Of Contents" with python-docx/lxml -


i'm trying automate creation of .docx files (wordml) of python-docx (https://github.com/mikemaccana/python-docx). current script creates toc manually following loop:

for chapter in mychapters:     body.append(paragraph(chapter.text, style='listnumber')) 

does know of way use "word built-in" toc-function, adds index automatically , creates paragraph-links individual chapters?

thanks lot!

the key challenge rendered toc depends on pagination know page number put each heading. pagination function provided layout engine, complex piece of software built word client. writing page layout engine in python not idea, not project i'm planning undertake anytime :)

the toc composed of 2 parts:

  1. the element specifies toc placement , things heading levels include.
  2. the actual visible toc content, headings , page numbers dotted lines connecting them.

creating element pretty straightforward , relatively low-effort. creating actual visible content, @ least if want page numbers included, requires word layout engine.

these options:

  1. just add tag , few other bits signal word toc needs updated. when document first opened, dialog box appears saying links need refreshed. user clicks yes , bob's uncle. if user clicks no, toc title appears no content below , toc can updated manually.

  2. add tag , engage word client, means of c# or visual basic against word automation library, open , save file; fields (including toc field) updated.

  3. do same thing server-side if have sharepoint instance or whatever can word automation services.

  4. create autoopen macro in document automatically runs field update when document opened. won't pass lot of virus checkers , won't work on locked-down windows builds common in corporate setting.

here's a nice set of screencasts eric white explain hairy details


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -