如何阅读目录的“目录”来自MS Word文档使用Win32COM客户端?
最近,我开始研究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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论