Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python更新MS Word.docx文档的TOC(目录)_Python_Pywin32_Win32com_Python Docx - Fatal编程技术网

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

用Python更新MS Word.docx文档的TOC(目录),python,pywin32,win32com,python-docx,Python,Pywin32,Win32com,Python Docx,我使用python包“PythonDocx”修改MS word.docx文档的结构和内容。该软件包无法更新目录[ 是否存在更新文档TOC的变通方法?我考虑过使用python包“pywin32”[或为MS Office提供“cli控制”功能的类似pypi包]中的“win32com.client” 我尝试了以下方法: 我将document.docx更改为document.docm,并实现了以下宏[: 如果我更改内容(添加/删除标题)并运行宏,TOC将更新。我保存了文档,我很高兴 我实现了以下pyth

我使用python包“PythonDocx”修改MS word.docx文档的结构和内容。该软件包无法更新目录[

是否存在更新文档TOC的变通方法?我考虑过使用python包“pywin32”[或为MS Office提供“cli控制”功能的类似pypi包]中的“win32com.client”

我尝试了以下方法:

我将document.docx更改为document.docm,并实现了以下宏[:

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

我实现了以下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_文件)在更高级别的脚本中调用(该脚本操作文档的toc相关内容)。此函数调用后,文档保存(doc.Save())、关闭(doc.Close())并关闭word实例(word.Quit())。但toc不会更新


在执行宏指令后,MS Word会执行额外的动作吗?我没有考虑到

< P>来更新TOC,这对我来说是有效的:

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

下面是一个更新word 2013.docx文档TOC的片段,该文档只包含一个目录(例如,只有标题TOC,没有数字TOC等)。如果脚本update\u TOC.py是从命令promt运行的(windows 10,命令promt不是“以管理员身份运行”)使用
python update\u toc.py
python的系统安装在同一目录下打开文件doc\u和\u toc.docx,更新toc(在我的例子中是标题)并将更改保存到同一文件中。该文档可能无法在Word 2013的另一个实例中打开,也可能没有写保护。请注意,此脚本确实存在

更新目录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()

我使用docxtplpython包自动生成一个docx文件。 此文档包含许多自动生成的表

我需要在模板生成后更新整个文档(以刷新生成的表号以及内容表、图和表)。 我不熟悉VBA,不知道用于此更新的函数。为了找到它们,我通过“录制宏”按钮创建了word宏。 我将自动生成的代码翻译成python,结果如下。 我需要一种可以帮助通过python执行任何word操作的工具

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()

这段代码片段在我的项目中无法正常工作。您是否专门选择了word文档?如何选择?可能是word的不同实例/进程的处理是我的问题…win32com以窗口为中心,这在ubuntu或mac上不起作用。有没有其他平台可以实现这一点?您有机会用win32com?在python中创建TOC的代码。我记不起来了,也不确定是否有关于这方面的注释。当您打开一个单独的问题时,请告诉我,我会看一看……以下是我打开的问题:
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()