【问题标题】:Update the TOC (table of content) of MS Word .docx documents with Python使用 Python 更新 MS Word .docx 文档的 TOC(目录)
【发布时间】:2015-10-07 12:39:14
【问题描述】:

我使用python包“python-docx”来修改MS word .docx文档的结构和内容。该软件包无法更新 TOC(目录)[Python: Create a "Table Of Contents" with python-docx/lxml

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

我尝试了以下方法:

我将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

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

我实现了以下应该等效于宏的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...')

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

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

【问题讨论】:

    标签: python pywin32 win32com python-docx


    【解决方案1】:

    这是一个 sn-p,用于更新 word 2013 .docx 文档的 TOC,该文档仅包含一个目录(例如,只有标题的 TOC,没有数字的 TOC 等)。如果脚本 update_toc.py 是使用python update_toc.py从命令提示符(windows 10,命令提示符不是“以管理员身份运行”)运行的,python 的系统安装会打开文件 doc_with_toc.docx 在同一目录中,更新目录(在我的情况下为标题)并将更改保存到同一文件中。该文档可能无法在另一个 Word 2013 实例中打开,并且可能没有写保护。请注意,此脚本执行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()
    

    【讨论】:

    • 你有没有机会用win32com在python中创建一个TOC?
    • 我不记得了,我不确定我是否对此有一些注释。当你打开一个单独的问题时让我知道,我会看看......
    • 这是我打开的问题:stackoverflow.com/questions/42820704/…
    【解决方案2】:

    我使用 docxtpl python 包自动生成一个 docx 文件。 该文档包含许多自动生成的表格。

    我需要在模板生成后更新整个文档(以刷新我生成的表格编号以及内容、图形和表格的表格)。 我不精通 VBA,也不知道用于此更新的功能。为了找到它们,我通过“记录宏”按钮创建了一个单词宏。 我将自动生成的代码翻译成 python,结果如下。 我可以帮助通过python执行任何单词操作的东西。

    def DocxUpdate(docx_file):
        word = win32com.client.DispatchEx("Word.Application")
        doc = word.Documents.Open(docx_file)
    
        # update all figure / table numbers
        word.ActiveDocument.Fields.Update()
    
        # update Table of content / figure / table    
        word.ActiveDocument.TablesOfContents(1).Update()
        word.ActiveDocument.TablesOfFigures(1).Update()
        word.ActiveDocument.TablesOfFigures(2).Update()
    
        doc.Close(SaveChanges=True)
    
        word.Quit()
    

    【讨论】:

      【解决方案3】:

      要更新 TOC,这对我有用:

      word = win32com.client.DispatchEx("Word.Application")
      Selection = word.Selection 
      Selection.Fields.Update
      

      【讨论】:

      • 此代码 sn-p 在我的项目中无法正常工作。您是否专门选择了word文档以及如何选择?在我的情况下,处理不同的单词实例/过程可能是问题......
      • win32com 以窗口为中心,这在 ubuntu 或 mac 上不起作用。有没有办法在其他平台上实现这一点?
      猜你喜欢
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 2016-10-31
      • 2016-07-16
      相关资源
      最近更新 更多