3

我正在尝试使用 RevitPythonShell 将数据从 Revit 写入 Excel。

到目前为止,我已经收集了压缩列表中的所有数据,并进行了枚举 for 循环,以将数据写入相应的行和列,如下所示:

for index, data in enumerate(wall_zipped_list):
    for count, parameters in enumerate(data):
        wall_cell = worksheet_wanden.Range[(str(column_list[count]))+str(index+5)]      
        wall_cell.Value2 = data[count]

这非常慢,因为循环Value2每次都在调用。包含大约 800 面墙的 Revit 模型需要 2 分钟才能写入 Excel。所以我尝试了一种使用字典的不同方法。

for k , v in data_dict.iteritems():
    #print k, v[0]
    worksheet_wanden.Range["A"+str(count)].Value2 = k
    worksheet_wanden.Range["B"+str(count)].Value2 = v[0]
    worksheet_wanden.Range["C"+str(count)].Value2 = v[1]
    worksheet_wanden.Range["D"+str(count)].Value2 = v[2]
    worksheet_wanden.Range["E"+str(count)].Value2 = v[3]
    worksheet_wanden.Range["F"+str(count)].Value2 = v[4]
    worksheet_wanden.Range["G"+str(count)].Value2 = v[5]
    worksheet_wanden.Range["H"+str(count)].Value2 = v[6]
    worksheet_wanden.Range["I"+str(count)].Value2 = v[7]
    worksheet_wanden.Range["J"+str(count)].Value2 = v[8]
    worksheet_wanden.Range["K"+str(count)].Value2 = v[9]
    count += 1

这种方法已经快很多了。这需要大约 20 秒才能在 Excel 中用 10 列填充大约 800 行。我是否缺少一些 IronPython 功能,您可以将字典或列表写入 Excel 行或列?

我还研究了安装 3d 派对模块。但这并不是一个真正的选择,因为 RevitPythonShell 使用 IronPython 2.7.3,我无法让 pip install 工作。

提前致谢。

首先在 IronPython 中写入 csv 然后以某种方式将其导入到 excel 中可能会更快吗?

4

3 回答 3

2

这更多是关于 .NET/Excel 互操作的问题。我认为,基于这个 SO question,您应该能够将数组分配给一个范围。

也就是说,您当前的范围只是一个单元格。您可以尝试创建 2dSystem.Array并将其分配给范围...我在这里尝试过:

``` 导入 clr clr.AddReference("Microsoft.Office.Interop.Excel")

import Microsoft.Office.Interop.Excel as Excel excel = Excel.ApplicationClass() excel.Visible = True # 使 Excel 应用程序对用户可见 workbook = excel.Workbooks.Add() worksheet = workbook.Worksheets.Add()

从系统导入数组 xlrange = worksheet.Range["A1:c3"]

a = Array.CreateInstance(object, 3, 3) i = 0 对于范围内的行(3):对于范围内的列(3):a[row, column] = i i += 1

xlrange.Value2 = 一个```

这会产生如下结果:

Excel 范围的屏幕截图

关于 IronPython 和 Excel 的更多信息可以在这里找到:http ://www.ironpython.info/index.php?title=Interacting_with_Excel

您可以尝试安装xlwt模块并使用它 - 避免 COM 互操作。

于 2017-12-18T10:03:01.910 回答
0

有用

i = 0 

xlrange_revit_type = worksheet_data.Range["C2:C" + str(len(revit_type_list)+1)]
a = Array.CreateInstance(object,len(revit_type_list), 3) 

while i < len(revit_type_list):
    a[i,0] = revit_type_list[i]
    i += 1

xlrange_revit_type.Value2 = a

是否可以为 RevitPythonShell 安装像 xlwt 这样的 3d 派对模块?我找不到任何文档。

于 2017-12-21T09:37:48.343 回答
0

如果您指的是可以在 VB 中使用的 Cells 表示法,我不知道它为什么不起作用,但您可以创建一个定义:

def Cells(a,b):
    return str(chr(a+96) + str(b))

现在你可以调用类似的东西:

x1range = ws.Range(Cells(1,1),Cells(5,10))

它的工作原理是一样的。这就是我所做的。

于 2019-04-24T20:19:31.780 回答