使用 Python 更新 MS Word .docx 文档的 TOC(目录)

thi*_*ybk 7 python pywin32 win32com python-docx

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

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

我尝试了以下方法:

我将 document.docx 更改为 document.docm 并实现了以下宏 [ 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
Run Code Online (Sandbox Code Playgroud)

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

我实现了以下 python 代码,它应该相当于宏:

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...')
Run Code Online (Sandbox Code Playgroud)

update_toc(docx_file) 在更高级别的脚本中调用(它操作文档的 TOC 相关内容)。调用此函数后,将保存文档 (doc.Save())、关闭 (doc.Close()) 并关闭 word 实例 (word.Quit())。但是目录未更新。

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

thi*_*ybk 8

以下是更新 word 2013 .docx 文档目录的片段,该文档仅包含一个目录(例如,仅包含标题目录,没有图表目录等)。如果脚本update_toc.py从命令提示符(Windows 10,命令提示符不是“以管理员身份运行”)运行,则使用python update_toc.pypython 的系统安装打开文件doc_with_toc.docx在同一目录中,更新目录(在我的情况下是标题)并将更改保存到同一文件中。该文档可能无法在 Word 2013 的其他实例中打开,并且可能没有写保护。请注意,此脚本与选择整个文档内容并按 F9 键不同

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()
Run Code Online (Sandbox Code Playgroud)