I am trying to create levels and views from excel file, but scripts fails and keeps running continuously without an error
Code below,
import clr
import os
from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from Microsoft.Office.Interop import Excel
# Create Excel application and open the file
excel_app = Excel.ApplicationClass()
excel_app.Visible = False # Set to True if you want to see Excel
workbook = excel_app.Workbooks.Open(excel_file_path)
worksheet = workbook.Sheets[1] # Assuming first sheet
# Read the Excel data starting from the second row
row = 2
while True:
level_name = worksheet.Cells(row, 1).Value
elevation = worksheet.Cells(row, 2).Value
if not level_name or not elevation:
break # Exit if there is no more data
levels_data.append((level_name, elevation))
row += 1
# Clean up
workbook.Close(False)
excel_app.Quit()
return levels_data
Path to your Excel file (update this path accordingly)
with Transaction(doc, “Create Levels”) as trans:
trans.Start()
# Get all existing levels in the project to avoid duplicates
existing_levels = FilteredElementCollector(doc).OfClass(Level).ToElements()
existing_level_names = [level.Name for level in existing_levels]
# Iterate through the level data from Excel and create levels
for level_name, elevation in levels_data:
if level_name not in existing_level_names: # Check if the level already exists
new_level = Level.Create(doc, elevation)
new_level.Name = level_name # Set the level name from Excel data
trans.Commit()
I’m also trying to work with an Excel file, but it didn’t work for me since my PyRevit environment doesn’t have access to pandas, how did you overcome this?
@aaronrumple’s idea may well work (I’ve never tried it). Someone with more coding experience than me may well raise their eyebrows at this, but I need so many external packages in my Revit scripts that I ended up finding another solution.
I located the folder pyRevit is referencing for packages (C:\x\pyRevit-Master\site-packages)
I located the folder in which pandas (or any other package) is saved by running “pip show pandas” in my console.
I ran xcopy /E /I “current location of pandas” “C:\x\pyRevit-Master\site-packages” in the console to copy the contents of the former location into the latter. Whenever it asked me whether to overwrite duplicate files, I chose no.
At the end of this process, pyRevit had all the packages it needed to execute my scripts.
I have used a similar process with Dynamo, which also worked for me.
I haven’t tried OpenXML yet, I’ll try to see if that works for me.
The ‘problem’ which I would have when I would change these settings, is that every user of our PyRevit toolbar, would also have to perform these steps, which is far from ideal because we would like to have the least amount of overhead as possible, since most users are not that tech savvy.
Exactly the issue with using custom packages. You need a deployment plan. If you are an office with limited IT support, this can be time consuming. If everyone has Revit and Excel installed, then Interop and OpenXML area already installed and available. Interop is installed with Office. The OpenXML.dll is installed with Dynamo as well as with Office.