Python:创建“目录"使用python-docx/lxml [英] Python: Create a "Table Of Contents" with python-docx/lxml

查看:1202
本文介绍了Python:创建“目录"使用python-docx/lxml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试借助python-docx(. https://github.com/mikemaccana/python-docx ).我当前的脚本通过以下循环手动创建ToC:

I'm trying to automate the creation of .docx files (WordML) with the help of python-docx (https://github.com/mikemaccana/python-docx). My current script creates the ToC manually with following loop:

for chapter in myChapters:
    body.append(paragraph(chapter.text, style='ListNumber'))

有人知道使用内置单词" ToC功能的方法吗?该功能会自动添加索引,并为各个章节创建段落链接?

Does anyone know of a way to use the "word built-in" ToC-function, which adds the index automatically and also creates paragraph-links to the individual chapters?

非常感谢!

推荐答案

主要挑战在于,呈现的ToC依赖于分页才能知道为每个标题放置的页码.分页是布局引擎提供的功能,布局引擎是Word客户端中内置的非常复杂的软件.用Python编写页面布局引擎可能不是一个好主意,绝对不是我计划很快进行的项目:)

The key challenge is that a rendered ToC depends on pagination to know what page number to put for each heading. Pagination is a function provided by the layout engine, a very complex piece of software built into the Word client. Writing a page layout engine in Python is probably not a good idea, definitely not a project I'm planning to undertake anytime soon :)

ToC由两部分组成:

The ToC is composed of two parts:

  1. 指定ToC位置以及要包含的标题级别之类的元素.
  2. 实际可见的ToC内容,标题和页码以及连接它们的虚线.

创建元素非常简单且省力.创建实际的可见内容(至少如果要包含页码,则需要Word布局引擎).

Creating the element is pretty straightforward and relatively low-effort. Creating the actual visible content, at least if you want the page numbers included, requires the Word layout engine.

这些是选项:

  1. 只需添加标签和其他一些位以向Word发出信号,表明ToC需要更新.首次打开文档时,将出现一个对话框,指出需要刷新链接.用户单击是,鲍勃是您的叔叔.如果用户单击否",则ToC标题将显示,其下方没有任何内容,并且ToC可以手动更新.

  1. Just add the tag and a few other bits to signal Word the ToC needs to be updated. When the document is first opened, a dialog box appears saying links need to be refreshed. The user clicks Yes and Bob's your uncle. If the user clicks No, the ToC title appears with no content below it and the ToC can be updated manually.

添加标签,然后使用C#或Visual Basic针对Word Automation库使用Word客户端,以打开并保存文件;所有字段(包括ToC字段)都会更新.

Add the tag and then engage a Word client, by means of C# or Visual Basic against the Word Automation library, to open and save the file; all the fields (including the ToC field) get updated.

如果您有SharePoint实例或可以使用Word Automation Services进行的操作,请在服务器端执行相同的操作.

Do the same thing server-side if you have a SharePoint instance or whatever that can do it with Word Automation Services.

在文档中创建一个AutoOpen宏,该宏在打开文档时自动运行字段更新.可能不会通过很多病毒检查程序,并且无法在公司环境中常见的锁定Windows版本上工作.

Create an AutoOpen macro in the document that automatically runs the field update when the document is opened. Probably won't pass a lot of virus checkers and won't work on locked-down Windows builds common in a corporate setting.

这是非常埃里克·怀特(Eric White)精心制作的一组屏幕录像,解释了所有毛茸茸的细节

这篇关于Python:创建“目录"使用python-docx/lxml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆