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.

Arnaud
  • 445
  • 4
  • 18

2 Answers2

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

Community
  • 1
  • 1
Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
  • 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 Apr 21 '17 at 09:47
  • @Arnaud do you have Excel installed? – Daren Thomas Apr 23 '17 at 17:55
  • 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! – Arnaud May 02 '17 at 08:13
  • yes, it should be possible with the `xlrd` module. I'm not sure if this will work from IronPython, but give it a try! – Daren Thomas May 03 '17 at 14:44
  • @Arnaud I've updated my answer to include a hint to a module that can do this without Excel. Good luck! – Daren Thomas May 03 '17 at 14:47
3

Why use a proprietary file format like XLS instead of something sensible, simple and open like CSV?

https://en.wikipedia.org/wiki/Comma-separated_values

Jeremy Tammik
  • 7,333
  • 2
  • 12
  • 17
  • It is indeed what I am doing so far, but the XLS file will be subject to many changes over time, and I wanted to avoid re-exporting to csv each time. Thanks! – Arnaud Apr 21 '17 at 09:10