Writing text to a Word Document

Mike Driscoll kyosohma at gmail.com
Mon May 11 16:02:58 EDT 2009


On May 11, 11:27 am, gazath... at gmail.com wrote:
> Hi everyone,
>
> I am trying to write several attributes from a database table and
> using the code below I can write the values however it is only
> overwriting on the first line.
>
> I am new to the win32com bit and I would like to know what is the
> recommended reference to loop down the page and add multiple records
> rather than the worddoc.Content.
>
> I have read a little about writing this to an RTF or htmol but I am
> getting information overload and going nowhere.  Does anyone have a
> nice tutorial out there that simply explains how to do this?
>
> Much appreciated for any thoughts.
>
> Cheers,
>
> Gareth
>
> import win32com.client, arcgisscripting, sys, os
> gp = arcgisscripting.create(9.3)
>
> #Create word document component -http://www.faqts.com/knowledge_base/view.phtml/aid/37034/fid/244
> wordapp = win32com.client.Dispatch("Word.Application") # Create new
> Word Object
> wordapp.Visible = 1 # Word Application should`t be visible
> worddoc = wordapp.Documents.Add() # Create new Document Object
> worddoc.Content.Font.Size = 11
> worddoc.Content.Paragraphs.TabStops.Add (100)
>
> #Setup of GDB Connection
> # Get the Featureclass from the Mobel - Property Box
> PropBox = "C:\\Avon Fire Rescue\\data\AvonFire_1.gdb\\PropertyBox"
>
> # Make a search cursor for the Property Box Feature class
> PBselCur = gp.searchcursor(PropBox)
> PBrow = PBselCur.reset()
> PBrow = PBselCur.next()
>
> #Using the search cursor get the Premise ID from a Property box
>
> while PBrow:
>     PBrowVal = PBrow.getvalue("PremisesID")
>     #Add data to Word Document
>     worddoc.Content.Text = (PBrowVal)
>     PBrow = PBselCur.next()
>
> #worddoc.Close() # Close the Word Document (a save-Dialog pops up)
>
> print "Fin"

What you probably want is something like this:

rng = worddoc.Range(0,0)
while PBrow:
    rng.InsertAfter(PBrowVal + "\r\n")  # needs return and new line
character

At least, that's what I found in one of my books. It seemed to work
when I tested it in the interpreter.

Mike



More information about the Python-list mailing list