【问题标题】:Reading Excel files from Revit Python Shell从 Revit Python Shell 读取 Excel 文件
【发布时间】:2017-09-15 09:08:18
【问题描述】:

我正在尝试使用 Revit Python Shell 从 Revit 2017 API 读取 Excel 文件。基本上,我不知道自己在做什么,但我试过这个:http://wiki.theprovingground.org/revit-api-py-excel,但我收到了一个错误:

Traceback(最近一次调用最后一次): 文件“”,第 1 行,在 EnvironmentError:System.Runtime.InteropServices.COMException(0x800401F3):无效的类字符串(来自 HRESULT 的异常:0x800401F3(CO 在 System.Runtime.InteropServices.Marshal.CLSIDFromProgID(字符串 progId,Guid & clsid) 在 System.Runtime.InteropServices.Marshal.GetActiveObject(字符串 progID) 在 Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame 框架) 在 Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame 框架) 在 Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,Tret](T0 arg0, T1 arg1, T2 arg2, T3 arg3) 在 IronPython.Compiler.Ast.CallExpression.Invoke1Instruction.Run(InterpretedFrame 框架) 在 Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame 框架) 在 Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,Tret](T0 arg0, T1 arg1) 在 IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx) 在 IronPython.Hosting.PythonCommandLine.c__DisplayClass1.b__0()

运行时: System.Runtime.InteropServices.Marshal.GetActiveObject('Excel.Application')

我在 Windows 7 机器上执行此操作。

除此之外,我几乎尝试了我在网上找到的每个模块,这些模块应该有助于打开 xlsx 文件,但每次我都会在某些时候出错。有人可以帮忙吗?它也可以是 ods 文件。

谢谢! 阿诺。

【问题讨论】:

    标签: revitpythonshell pyrevit


    【解决方案1】:

    您使用的代码 (System.Runtime.InteropServices.Marshal.GetActiveObject('Excel.Application')) 假定 Excel 已经在运行。

    查看此页面以了解其他方法:http://www.ironpython.info/index.php?title=Interacting_with_Excel

    基本上,试试这个: import clr clr.AddReference("Microsoft.Office.Interop.Excel") import Microsoft.Office.Interop.Excel as Excel excel = Excel.ApplicationClass()

    如果你想获得一个活动实例(如果有的话),你可以在try/except 块中使用你的第一个方法,如果没有找到,则只创建一个新的 excel 实例。

    另一种选择是使用模块xlrd - 这样您就不需要安装 Excel。 This question discusses some issues with using xlrd in IronPython

    【讨论】:

    • 谢谢!好吧,我试过了,但现在有:“异常:System.Runtime.InteropServices.COMException (0x80040154):检索具有 CLSID {0002450 0-0000-0000-C000-000000000046} 的组件的 COM 类工厂因以下错误而失败:80040154 类未注册(HRESULT 异常:0x80040154 (REGDB_E_CLASSNOTREG))。”。我用谷歌搜索了一下,但这超出了我的知识范围。如果您有想法,我会接受;)否则,我将辞职以使用 CSV。还是非常感谢!
    • @Arnaud 你安装了 Excel 吗?
    • 你好达伦,这是一个很好的问题;)答案是否定的。我觉得很愚蠢,但我希望能够在没有工作安装的情况下打开它(有点像打开办公室可以打开 Excel 文件..)。你认为这可能吗?谢谢!
    • 是的,xlrd 模块应该可以。我不确定这是否适用于 IronPython,但请试一试!
    • @Arnaud 我已经更新了我的答案,以包含一个无需 Excel 即可执行此操作的模块的提示。祝你好运!
    【解决方案2】:

    为什么要使用像 XLS 这样的专有文件格式,而不是像 CSV 这样的合理、简单和开放的文件格式?

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

    【讨论】:

    • 这确实是我目前所做的,但是随着时间的推移,XLS 文件会发生很多变化,我想避免每次都重新导出到 csv。谢谢!
    猜你喜欢
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    相关资源
    最近更新 更多