Skip to content

Instantly share code, notes, and snippets.

@wknowles
Last active January 14, 2026 14:55
Show Gist options
  • Select an option

  • Save wknowles/b6a98bf60cb68484fe1065c9488e8181 to your computer and use it in GitHub Desktop.

Select an option

Save wknowles/b6a98bf60cb68484fe1065c9488e8181 to your computer and use it in GitHub Desktop.
python script to add to revit dynamo
import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
# Inputs from Dynamo nodes
excel_data = IN[0] # Full Excel data including headers (list of lists)
rooms = IN[1] # All Room elements from Revit
# Initialize output lists
updated_rooms = []
success_count = 0
failed_rooms = []
messages = []
# Extract headers (parameter names) and data
if len(excel_data) > 0:
headers = excel_data[0] # First row contains parameter names
data_rows = excel_data[1:] # Remaining rows contain data
# Find the index of the "Number" column
try:
number_col_index = headers.index("Number")
except ValueError:
OUT = "Error: 'Number' column not found in Excel headers", [], []
raise Exception("Excel file must have a 'Number' column")
# Create a dictionary mapping room numbers to their data
room_data_dict = {}
for row in data_rows:
if len(row) > number_col_index:
room_number = str(row[number_col_index]).strip()
room_data_dict[room_number] = row
# Start transaction
TransactionManager.Instance.EnsureInTransaction(DocumentManager.Instance.CurrentDBDocument)
# Process each room
for room in rooms:
try:
# Unwrap the Dynamo element to get the Revit element
revit_room = UnwrapElement(room)
# Get room number from Revit
room_number_param = revit_room.Number
room_number = str(room_number_param).strip() if room_number_param else ""
# Check if this room number exists in Excel data
if room_number in room_data_dict:
excel_row = room_data_dict[room_number]
params_updated = 0
# Loop through each column and update corresponding parameter
for i, header in enumerate(headers):
# Skip the Number column (already matches)
if header == "Number" or i >= len(excel_row):
continue
# Get the value from Excel
value = excel_row[i]
# Skip empty/null values
if value is None or str(value).strip() == "":
continue
# Try to set the parameter
try:
# Unwrap the Dynamo element to get the Revit element
revit_room = UnwrapElement(room)
param = revit_room.LookupParameter(header)
if param and not param.IsReadOnly:
# Handle different parameter types
if param.StorageType == StorageType.String:
param.Set(str(value))
params_updated += 1
elif param.StorageType == StorageType.Integer:
param.Set(int(float(value)))
params_updated += 1
elif param.StorageType == StorageType.Double:
param.Set(float(value))
params_updated += 1
else:
if not param:
messages.append("Room {}: Parameter '{}' not found".format(
room_number, header))
except Exception as e:
messages.append("Room {}: Failed to set '{}' - {}".format(
room_number, header, str(e)))
if params_updated > 0:
updated_rooms.append(room)
success_count += 1
messages.append("Room {}: Updated {} parameters".format(
room_number, params_updated))
else:
failed_rooms.append(room_number)
messages.append("Room {}: Not found in Excel data".format(room_number))
except Exception as e:
try:
revit_room = UnwrapElement(room)
room_num = revit_room.Number if hasattr(revit_room, 'Number') else "Unknown"
except:
room_num = "Unknown"
messages.append("Room {}: Error - {}".format(room_num, str(e)))
# End transaction
TransactionManager.Instance.TransactionTaskDone()
# Prepare summary
summary = "Successfully updated {} of {} rooms".format(success_count, len(rooms))
if failed_rooms:
summary += "\nRooms not found in Excel: {}".format(", ".join(failed_rooms[:10]))
if len(failed_rooms) > 10:
summary += "... and {} more".format(len(failed_rooms) - 10)
else:
summary = "Error: No data found in Excel file"
messages = []
# Output
OUT = summary, updated_rooms, messages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment