I am very shy at the moment. Based Can I use Win32 COM to replace text inside a Word document? I was able to encode a simple template system that generates docs words from a doc template dictionary (in Python).
My problem is that the text in the "Text Fields" was not found this way. Even in the Word itself there is no way to search for everything - you really need to choose between the “Main Document” and “Text Fields”. As a newbie in the Windows world, I tried to look at VBA documents for it, but did not find any help (perhaps due to the fact that the “text box” is a very common term).
word.Documents.Open(f) wdFindContinue = 1 wdReplaceAll = 2 find_str = '\{\{(*)\}\}' find = word.Selection.Find find.Execute(find_str, False, False, True, False, False, \ True, wdFindContinue, False, False, False) while find.Found: t = word.Selection.Text.__str__() r = process_placeholder(t, answer_data, question_data) if type(r) == dict: errors.append(r) else: find.Execute(t, False, True, False, False, False, \ True, False, False, r, wdReplaceAll)
This is the relevant part of my code. I already managed to get around all the problems (hint: if you want to replace strings with more than 256 characters, you need to do this through the clipboard, etc.)
python vba ms-word win32com
Mark
source share