简体   繁体   中英

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. I have seen examples only like "word.Documents.Open" after Dispatch.

I can do it with excel (I can access the opened MS Excel file on "the fly") for example:

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?

I hope I was understandable. Thank you,

Finally, I got it. The key is: "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)  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM