使用 Python 更新 MS Word .docx 文档的 TOC(目录) [英] Update the TOC (table of content) of MS Word .docx documents with Python

查看:65
本文介绍了使用 Python 更新 MS Word .docx 文档的 TOC(目录)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用python包python-docx"来修改MS word .docx文档的结构和内容.该软件包无法更新 TOC(目录)[Python:创建目录"使用 python-docx/lxml.

I use the python package "python-docx" to modify the structure amd content of MS word .docx documents. The package lacks the possibility to update the TOC (table of content) [Python: Create a "Table Of Contents" with python-docx/lxml.

是否有更新文档 TOC 的解决方法?我想过使用 python 包pywin32"中的win32com.client"[https://pypi.python.org/pypi/pypiwin32] 或类似的 pypi 包,为 MS Office 提供cli 控制"功能.

Are there workarounds to update the TOC of a document? I thought about using "win32com.client" from the python package "pywin32" [https://pypi.python.org/pypi/pypiwin32] or a comparable pypi package offering "cli control" capabilities for MS Office.

我尝试了以下方法:

我将 document.docx 更改为 document.docm 并实现了以下宏 [http://word.tips.net/T000301_Updating_an_Entire_TOC_from_a_Macro.html]:

I changed the document.docx to document.docm and implemented the following macro [http://word.tips.net/T000301_Updating_an_Entire_TOC_from_a_Macro.html]:

Sub update_TOC()

If ActiveDocument.TablesOfContents.Count = 1 Then _
  ActiveDocument.TablesOfContents(1).Update

End Sub

如果我更改内容(添加/删除标题)并运行宏,TOC 会更新.我保存了文档,我很高兴.

If i change the content (add/remove headings) and run the macro the TOC is updated. I save the document and i am happy.

我实现了以下应该与宏等效的python代码:

I implement the following python code which should be equivalent to the macro:

import win32com.client

def update_toc(docx_file):
    word = win32com.client.DispatchEx("Word.Application")
    doc = word.Documents.Open(docx_file)
    toc_count = doc.TablesOfContents.Count
    if toc_count == 1:
        toc = doc.TablesOfContents(1)
        toc.Update
        print('TOC should have been updated.')
    else:
        print('TOC has not been updated for sure...')

update_toc(docx_file) 在更高级别的脚本中调用(操作文档的 TOC 相关内容).在此函数调用之后,文档被保存 (doc.Save())、关闭 (doc.Close()) 并且单词实例被关闭 (word.Quit()).然而,TOC 并未更新.

update_toc(docx_file) is called in a higher-level script (which manipulates the TOC-relevant content of the document). After this function call the document is saved (doc.Save()), closed (doc.Close()) and the word instance is closed (word.Quit()). However the TOC is not updated.

ms word 是否会在宏执行后执行我没有考虑的其他操作?

Does ms word perform additional actions after macro execution which i did not consider?

推荐答案

这里是更新 word 2013 .docx 文档的 TOC 的片段,该文档仅包含一个目录(例如,只有标题的 TOC,没有数字的 TOC等等.).如果使用 python update_toc.py 从命令提示符(windows 10,命令提示符不是以管理员身份运行")运行脚本 update_toc.py,python 的系统安装将打开文件 doc_with_toc.docx 在同一目录中,更新 TOC(在我的例子中是标题)并将更改保存到同一文件中.该文档可能无法在 Word 2013 的另一个实例中打开,并且可能没有写保护.请注意,此脚本执行 与选择整个文档内容并按 F9 键不同.

Here is a snippet to update the TOC of a word 2013 .docx document which includes only one table of content (e.g. just TOC of headings, no TOC of figures etc.). If the script update_toc.py is run from the command promt (windows 10, command promt not "running as admin") using python update_toc.py the system installation of python opens the file doc_with_toc.docx in the same directory, updates the TOC (in my case the headings) and saves the changes into the same file. The document may not be opened in another instance of Word 2013 and may not be write-protected. Be aware of that this script does not the same as selecting the whole document content and pressing the F9 key.

update_toc.py 的内容:

import win32com.client
import inspect, os

def update_toc(docx_file):
    word = win32com.client.DispatchEx("Word.Application")
    doc = word.Documents.Open(docx_file)
    doc.TablesOfContents(1).Update()
    doc.Close(SaveChanges=True)
    word.Quit()

def main():
    script_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
    file_name = 'doc_with_toc.docx'
    file_path = os.path.join(script_dir, file_name)
    update_toc(file_path)

if __name__ == "__main__":
    main()

这篇关于使用 Python 更新 MS Word .docx 文档的 TOC(目录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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