Hello,
I am trying to do something pretty complicated so any help is appreciated.
Basically I have a word document which I use as a template. I would like to use the Microsoft.Office.Interop.Word API to replace text within that document with information from the current Revit document.
I’ve seen this IronPython post and would like to use it in a Python node: http://www.ironpython.info/index.php?title=Replace_Text_within_a_Word_document
Here’s my code so far:
#Some code taken from https://gist.github.com/ejstembler/1049552
import clr
import sys
import System
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.Wd.Replace.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)
time.sleep(3)
document.SaveAs(destination_filename)
document.Close()
document = None
word_application.Quit()
word_application = None
#The inputs to this node will be stored as a list in the IN variables.
file_name = IN[0]
search_terms = IN[1]
replace_terms = IN[2]
destination_name = IN[3]
doc_replace_text(file_name, search_terms, replace_terms, destination_name)
#Assign your output to the OUT variable.
OUT = 0
The Python node crashes and says this:
Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 49, in
File “”, line 13, in doc_replace_text
AttributeError: attribute ‘Wd’ of ‘namespace#’ object is read-only
If anyone could help me with this, that would be great.
Still new to all this so please excuse confusion I’m sure to have.
Thanks!