import win32com.client

wordapp = win32com.client.Dispatch("Word.Application") # Create new Word Object
wordapp.Visible = 0 # Word Application should`t be visible
worddoc = wordapp.Documents.Add() # Create new Document Object
worddoc.PageSetup.Orientation = 1 # Make some Setup to the Document:
worddoc.PageSetup.LeftMargin = 20
worddoc.PageSetup.TopMargin = 20
worddoc.PageSetup.BottomMargin = 20
worddoc.PageSetup.RightMargin = 20
worddoc.Content.Font.Size = 11
worddoc.Content.Paragraphs.TabStops.Add (100)
worddoc.Content.Text = "Hello"
worddoc.Content.Text=" I am a text!"
worddoc.Close() # Close the Word Document (a save-Dialog pops up)
wordapp.Quit() # Close the Word Application

The word doc from the above code will only contain " I am a text!" I would like to know how i can add/append text onto a word doc so that i have both "Hello" and " I am a text!" in the word doc.

Plz help me.......

10 Years
Discussion Span
Last Post by tzushky


I'm not going to install this module but I have a hunch: what if you do:

worddoc.Content.Text = "Hello"
worddoc.Content.Text +=" I am a text!"

instead of your lines. Maybe the Text attribute is only evaluated at the end of the rest of manipulations, before closing the app. If I'm right, then You overwrite the "Hello" with "I am a text!".

Another solution is simply to make one big string elsewhere and pass it to text:

worddoc.Content.Text = my_string_built_elsewhere

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.