如何阅读目录的“目录”来自MS Word文档使用Win32COM客户端?

发布于 2025-01-23 02:58:17 字数 622 浏览 0 评论 0原文

最近,我开始研究Python和MS Word。我尝试使用win32com.client Python模块(Pywin32)在.docx文档中更新目录。

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

现在,我正在尝试使用win32com.client Q1读取更新的目录

:请分享您的建议或有关如何读取目录的建议。

Q2:在上面的代码块中,doc.close(savechanges = true)大约需要35秒来执行任务。我们可以实现的近距离有任何绩效改善吗?

update(@ds_london的输入):

读取内容表

doc.TablesOfContents(1).Range()

以上代码将目录返回单个字符串。

Recently i have started working on python and MS word. I tried to update the Table of contents in a .docx document using win32com.client python module(pywin32).

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

Now i am trying to look how to read the updated Table of Contents using win32com.client

Q1: Please share your suggestions or references on how to read the Table of Contents.

Q2: In the above code block doc.Close(SaveChanges=True) takes around 35 secs to perform the task. Is there any performance improvement in the Close block we can achieve?

Update (inputs from @DS_London):

To read the Table of Contents

doc.TablesOfContents(1).Range()

The above code returns the Table of Contents as a single String.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文