Trying to add multiple values to Word Doc with win32com

This is the place for queries that don't fit in any of the other categories.

Trying to add multiple values to Word Doc with win32com

Postby voodoochild » Thu May 16, 2013 3:45 pm

I'm trying to add multiple values to a word document using find and replace with win32com.client in Python. In the code below I can find and replace the first name, but not the last name as well. I don't know to pass multiple values to the document.

Code: Select all
import win32com.client

word = win32com.client.DispatchEx("Word.Application")
word.Visible = True
word.DisplayAlerts = 0
word.Documents.Open("C:\TEMP\Testing\Me.docx")
find = word.Selection.Find

find.Text = "First Name"

if find.Text == "First Name":
    find.Replacement.Text = "John"
    find.Execute(Replace=1, Forward=True)

find.Text = "Last Name"

if find.Text == "Last Name":
    find.Replacement.Text = "Smith"
    find.Execute(Replace=1, Forward=True)

word.ActiveDocument.SaveAs('C:\TEMP\Testing\Me2.docx')
word.Quit()#releases Word object from memory

voodoochild
 
Posts: 1
Joined: Thu May 16, 2013 1:43 pm

Return to General Coding Help

Who is online

Users browsing this forum: W3C [Linkcheck] and 4 guests