Is there a way to read docx files in R25+

I don’t mean this to be a post asking why isn’t my code working, but I think I should offer some background on what I’ve tried. If there is something I’m obviously doing wrong than please let me know, but my real question is is there any module that would allow my to read docx files in python 2.7 that also works with Revit 25 .net8 update?

I’ve tried several times to get Microsoft.Office.Interop.Word to work in Revit 25 but I haven’t gotten anything to work. I keep getting the error:

AttributeError: '__ComObject' object has no attribute 'Open'

This error is generated from this code:

import Microsoft.Office.Interop.Word as Word
from System.Type import Missing

# Debug toggles
globalDebug = True
disabledScript = False

def dprint(*printItems):
    if globalDebug:
        for pI in printItems:
            print(pI)

missing = Missing
word_application = Word.ApplicationClass()
word_application.Visible = False

# Get word doc
wordDoc = forms.pick_file(file_ext="docx",init_dir="J:",
                            multi_file = False, title = "Pick a .docx word document.")


dprint(word_application.Documents)
dprint(dir(word_application.Documents))
document = word_application.Documents.Open(wordDoc, missing, True)
wdoc = document.Range()

I’ve check what methods were available and this is what I get in Revit 25. Notice that the open methods are missing:

['Equals', 'GetHashCode', 'GetLifetimeService', 'GetType', 'InitializeLifetimeService', 'MemberwiseClone', 'ReferenceEquals', 'ToString', '__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

While this is what I get in Revit 24:

['Add', 'AddBlogDocument', 'AddOld', 'Application', 'CanCheckOut', 'CheckOut', 'Close', 'Count', 'CreateObjRef', 'Creator', 'Equals', 'GetHashCode', 'GetLifetimeService', 'GetType', 'InitializeLifetimeService', 'Item', 'MemberwiseClone', 'Open', 'Open2000', 'Open2002', 'OpenNoRepairDialog', 'OpenOld', 'Parent', 'ReferenceEquals', 'Save', 'ToString', '_NewEnum', '__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

I tried tinkering with that but gave up. I tried using python-docx but it appears that will not work with the python version pyRevit uses.

I’ve been told to look into openXML, but I didn’t know where to start and suspect that I would run into the same problem with python-docx which I think uses openXML?

Hi @Crapai ,
You could try to use an old version of python-docx that still supoorted python 2.

From the changelog, version 1.0.0 is the one that dropped python 2 support, so you can use the previous one (0.8.11).
Obviously you will miss the latest features and bug fixes, though

I installed the 8.11 version by that wants to import lxml. I installed that as well, didn’t know what version of that to install so I just did version 6.0.0 but when I runt he script I’m getting this issue now:

IronPython Traceback:
Traceback (most recent call last):
 File "L:\Revit\Development\PyRevit\DevTool\DevTool.extension\DevTools.tab\Development.Panel\SheetSpecOpenXML.pushbutton\SheetSpecOpenXML_script.py", line 16, in <module>
 File "C:\Users\cjr\AppData\Roaming\pyRevit-Master\site-packages\docx\__init__.py", line 3, in <module>
 File "C:\Users\cjr\AppData\Roaming\pyRevit-Master\site-packages\docx\api.py", line 14, in <module>
 File "C:\Users\cjr\AppData\Roaming\pyRevit-Master\site-packages\docx\package.py", line 9, in <module>
 File "C:\Users\cjr\AppData\Roaming\pyRevit-Master\site-packages\docx\opc\package.py", line 9, in <module>
 File "C:\Users\cjr\AppData\Roaming\pyRevit-Master\site-packages\docx\opc\part.py", line 12, in <module>
 File "C:\Users\cjr\AppData\Roaming\pyRevit-Master\site-packages\docx\opc\oxml.py", line 12, in <module>
ImportError: cannot import etree from lxml

Script Executor Traceback:
IronPython.Runtime.Exceptions.ImportException: cannot import etree from lxml
 at IronPython.Runtime.Importer.ImportNestedModule(CodeContext context, PythonModule module, String[] parts, Int32 current, List path)
 at IronPython.Runtime.Importer.ImportFrom(CodeContext context, Object from, String name)
 at Microsoft.Scripting.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
 at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
 at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptCode, ModuleOptions options)
 at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str)
 at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func`5 defaultLoader)
 at IronPython.Runtime.Importer.ImportFromPath(CodeContext context, String name, String fullName, List path)
 at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level)
 at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level)
 at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String fullName, PythonTuple from, Int32 level)
 at Microsoft.Scripting.Interpreter.FuncCallInstruction`5.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
 at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
 at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptCode, ModuleOptions options)
 at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str)
 at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func`5 defaultLoader)
 at IronPython.Runtime.Importer.ImportNestedModule(CodeContext context, PythonModule module, String[] parts, Int32 current, List path)
 at IronPython.Runtime.Importer.ImportModuleFrom(CodeContext context, Object from, String[] parts, Int32 current)
 at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level)
 at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level)
 at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String fullName, PythonTuple from, Int32 level)
 at Microsoft.Scripting.Interpreter.FuncCallInstruction`5.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
 at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
 at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptCode, ModuleOptions options)
 at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str)
 at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func`5 defaultLoader)
 at IronPython.Runtime.Importer.ImportNestedModule(CodeContext context, PythonModule module, String[] parts, Int32 current, List path)
 at IronPython.Runtime.Importer.ImportModuleFrom(CodeContext context, Object from, String[] parts, Int32 current)
 at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level)
 at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level)
 at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String fullName, PythonTuple from, Int32 level)
 at Microsoft.Scripting.Interpreter.FuncCallInstruction`5.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
 at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
 at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptCode, ModuleOptions options)
 at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str)
 at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func`5 defaultLoader)
 at IronPython.Runtime.Importer.ImportNestedModule(CodeContext context, PythonModule module, String[] parts, Int32 current, List path)
 at IronPython.Runtime.Importer.ImportModuleFrom(CodeContext context, Object from, String[] parts, Int32 current)
 at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level)
 at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level)
 at Microsoft.Scripting.Interpreter.FuncCallInstruction`7.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.LightLambda.Run8[T0,T1,T2,T3,T4,T5,T6,T7,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7)
 at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String fullName, PythonTuple from, Int32 level)
 at Microsoft.Scripting.Interpreter.FuncCallInstruction`5.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
 at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
 at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptCode, ModuleOptions options)
 at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str)
 at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func`5 defaultLoader)
 at IronPython.Runtime.Importer.ImportNestedModule(CodeContext context, PythonModule module, String[] parts, Int32 current, List path)
 at IronPython.Runtime.Importer.ImportModuleFrom(CodeContext context, Object from, String[] parts, Int32 current)
 at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level)
 at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level)
 at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String fullName, PythonTuple from, Int32 level)
 at Microsoft.Scripting.Interpreter.FuncCallInstruction`5.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
 at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
 at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptCode, ModuleOptions options)
 at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, String name, String path)
 at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str)
 at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func`5 defaultLoader)
 at IronPython.Runtime.Importer.ImportFromPath(CodeContext context, String name, String fullName, List path)
 at IronPython.Runtime.Importer.ImportTopAbsolute(CodeContext context, String name)
 at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level)
 at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level)
 at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String fullName, PythonTuple from, Int32 level)
 at Microsoft.Scripting.Interpreter.FuncCallInstruction`5.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
 at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
 at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
 at Microsoft.Scripting.Hosting.CompiledCode.Execute(ScriptScope scope)
 at PyRevitLabs.PyRevit.Runtime.IronPythonEngine.Execute(ScriptRuntime& runtime)