[python-win32] i have this code, how do i passed to word

Tim Roberts timr at probo.com
Fri Jul 25 20:49:38 CEST 2008


Emanuel Sotelo wrote:
> hello Tim
> yes that is something that i did, it was printed in the idle and a 
> cut-and-paste to word.
> what i was wondering if it is posible to print the result in word 
> instead of idle?
> what line codes do i need to make this hapend?

I hope you do not think I am being stubborn, but the right answer 
depends on what your REAL task is.  It IS possible to automate Word, but 
it is rather tedious, and in many cases there are better ways to 
accomplish your task.  If this is for a one-time thing, where you just 
need to print the list, then what you did is exactly the right way.  It 
isn't necessary (or beneficial) to automate everything.

If this is something you need to do once in a while, then have the 
script write the information to a .txt file, then open the .txt file in 
Word to do your formatting.

If you need to make a regular report out of this, then there are better 
ways than Word.  One excellent way to do this is to write your text as 
an HTML page, with appropriate <table> tags to lay it out exactly as you 
want, then launch the HTML page and let IE or Firefox do the 
formatting.  wxPython even includes a module called HtmlEasyPrinting to 
help with this.

If you really need to insert this text into an existing Word document, 
then you need to open the Word COM object and use the Word object 
model.  See here for hints:   
http://www.faqts.com/knowledge_base/view.phtml/aid/37034/fid/244

    word = win32com.client.EnsureDispatch( "Word.Application" )
    word.Visible = 1  # if you want to watch the process
    doc = app.Documents.Add()
    doc.Content.Text = "This is the string to add to the document."
    doc.Content.MoveEnd()
    doc.Close()
    word.Quit()

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list