1

I am trying to read an Excel file from Revit 2017 API, using Revit Python Shell. Basically, I have no idea of what I'm doing, but I tried this: http://wiki.theprovingground.org/revit-api-py-excel , but I'm getting an error:

Traceback (most recent call last): File "", line 1, in EnvironmentError: System.Runtime.InteropServices.COMException (0x800401F3): Invalid class string (Exception from HRESULT: 0x800401F3 (CO at System.Runtime.InteropServices.Marshal.CLSIDFromProgID(String progId, Guid& clsid) at System.Runtime.InteropServices.Marshal.GetActiveObject(String progID) at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3) at IronPython.Compiler.Ast.CallExpression.Invoke1Instruction.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.Hosting.PythonCommandLine.<>c__DisplayClass1.b__0()

when running: System.Runtime.InteropServices.Marshal.GetActiveObject('Excel.Application')

I am doing this on a Windows 7 machine.

On side of that, I tried pretty much every module I found on the web, that are supposed to help opening xlsx files, but every time I'm getting and error at some point. Can anybody help on that? It can be ods files as well.

Thanks! Arnaud.

1

1 Answer 1

4

The code you're using (System.Runtime.InteropServices.Marshal.GetActiveObject('Excel.Application')) assumes Excel is already running.

Check this page for a different approach: http://www.ironpython.info/index.php?title=Interacting_with_Excel

Basically, try this: import clr clr.AddReference("Microsoft.Office.Interop.Excel") import Microsoft.Office.Interop.Excel as Excel excel = Excel.ApplicationClass()

If you want to get an active instance (if any), you could use your first method in a try/except block and only create a new excel instance if none is found.

Another option is to use module xlrd - this way you won't need to have Excel installed. This question discusses some issues with using xlrd in IronPython

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! Well I tried this, but now have: "Exception : System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {0002450 0-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).". I googled it a bit, but this is way beyond my knwoledge. If you have an idea, I'll take it ;) Otherwise I'll just resign myself to use CSV. Thanks a lot anyways!
@Arnaud do you have Excel installed?
Hello Daren, well that's a very good question ;) The answer is no.. I feel foolish, but I was hoping to be able to open it without a working installation (kind of the way Open Office can open Excel files..). Do you think that's possible? Thanks!
yes, it should be possible with the xlrd module. I'm not sure if this will work from IronPython, but give it a try!
@Arnaud I've updated my answer to include a hint to a module that can do this without Excel. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.