简体   繁体   English

如何访问以编辑已打开的 DOCX(在 python 中使用 win32com.client)

[英]How to access for editing an ALREADY OPENED DOCX (using win32com.client in python)

I would like to make some "magic" in previously opened MS Docx (like a VS script), but I do not find how I can do it.我想在以前打开的 MS Docx (如 VS 脚本)中制作一些“魔法”,但我不知道该怎么做。 I have seen examples only like "word.Documents.Open" after Dispatch.在 Dispatch 之后,我只看到了像“word.Documents.Open”这样的例子。

I can do it with excel (I can access the opened MS Excel file on "the fly") for example:我可以使用 excel 来做到这一点(我可以“即时”访问打开的 MS Excel 文件),例如:

from win32com.client import Dispatch
xl = Dispatch("Excel.application")
xl.Visible = True
xl.Sheets["Action"].Select
current=xl.Sheets["Action"]
...

How I can do it in MS Word as well?我如何在 MS Word 中也能做到这一点?

I hope I was understandable.我希望我是可以理解的。 Thank you,谢谢,

Finally, I got it.最后我得到了它。 The key is: "word.ActiveDocument"关键是:“word.ActiveDocument”

Here is the code:这是代码:

from win32com.client import Dispatch

word = Dispatch('Word.Application')
word.Visible = 1
doc=word.ActiveDocument

for word_t in doc.Words:
     print(word_t)  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何关闭另一个应用程序打开的 Excel 文件? 将 python 与 win32com.client 一起使用 - How to close an Excel file opened by another application? Using python with win32com.client Python win32com.client和outlook - Python win32com.client and outlook 如何使用 python win32com.client 在 Excel 中添加折线? - How to Add polyline in Excel with python win32com.client? Python win32com.client和“with”语句 - Python win32com.client and “with” statement Python 使用 win32com.client 导出报告 Microsoft Access - Python Export Report Microsoft Access using win32com.client 如何使用 python 和 win32com.client 从 Outlook 的电子邮件项目中访问 c​​c 电子邮件地址 - How to access cc email address from email items from outlook using python and win32com.client 如何在 Python 中使用 win32com.client 保存 Outlook 中的附件? - How to save attachment from outlook using win32com.client in Python? Python如何使用win32com.client和Outlook插入图像? - Python how to insert images using win32com.client and outlook? 如何使用 win32com.client 通过 python 将 XLA 添加到 excel 中? - How do I get an XLA add in into excel through python using win32com.client? 如何使用Python和win32com.client在PowerPoint中操作形状(颜色)? - How can I manipulate shapes (colors) in PowerPoint using Python and win32com.client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM