6

I am pretty stumped at the moment. Based on Can I use Win32 COM to replace text inside a word document? I was able to code a simple template system that generates word docs out of a template word doc (in Python).

My problem is that text in "Text Fields" is not find that way. Even in Word itself there is no option to search everything - you actually have to choose between "Main Document" and "Text Fields". Being new to the Windows world I tried to browse the VBA docs for it but found no help (probably due to "text field" being 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 portion of my code. I was able to get around all problems by myself by now (hint: if you want to replace strings with more than 256 chars, you have to do it via clipboard, etc ...)

Community
  • 1
  • 1
Mark
  • 61
  • 3

1 Answers1

2

Maybe you can use the OpenOffice API using the UNO component technology. With the Python-UNO bridge you can connect to an OpenOffice instance running in headless mode. Look at the tutorial to get started.
This is maybe an overkill for your scenario but it's a very powerful and flexible solution.

Reto Aebersold
  • 16,306
  • 5
  • 55
  • 74
  • Ok. But OpenOffice can handle MS Word files quite good. So you can process Word files as well. I use this bridge to generate documents in various formats like doc, ppt, odt or pdf. – Reto Aebersold May 17 '10 at 22:00
  • Well, yes in a sense that installing Ooo (as well as other tools) is no available option. Anyway, thank you for your input. – Mark May 17 '10 at 22:14