Quantcast
Viewing all articles
Browse latest Browse all 5786

Iron Python read/write data to Microsoft Word (*.doc) files?

@agi.andre wrote:

I’m trying to create/populate a Word document .docx file with data from grasshopper.
Do anyone know how this can be done?

What I’ve found is this code for ironpython: https://gist.github.com/ejstembler/1049552

`import sys
import clr
import System
from System import DateTime

clr.AddReference(“Microsoft.Office.Interop.Word”)
import Microsoft.Office.Interop.Word as Word

def doc_replace_text(source_filename, tokens, values, destination_filename):

missing = System.Type.Missing
replaceAll = Word.WdReplace.wdReplaceAll

word_application = Word.ApplicationClass()
word_application.visible = False

document = word_application.Documents.Open(source_filename)

for i in range(len(tokens)):
    for r in document.StoryRanges:
        #print "i = %d, tokens[i] = %s, values[i] = %s" % (i, tokens[i], values[i])
        r.Find.Text = tokens[i]
        r.Find.Replacement.Text = values[i]
        r.Find.Wrap = Word.WdFindWrap.wdFindContinue
        r.Find.Execute(missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, replaceAll, missing, missing, missing, missing)

document.SaveAs(destination_filename)
document.Close()
document = None

word_application.Quit()
word_application = None`

But when trying to load the template document into the grasshopper python editor I’m getting a None return. Anyone who can spot the problem? Or won’t this simply work in grasshopper?
Thanks!

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 5786

Trending Articles