Python node not working in Revit 2022 but works in Revit 2023

Hi,

As the title says I have a script that reads data from excel sheets then populates another excel worksheet template. This script works fine in Revit 2023 Dynamo 2.16 but doesn’t work in Revit 2022 Dynamo 2.12.

  • All nodes feeding the script are working
  • IronPython engine is used for both
  • The python script runs without errors but doesn’t actually process, save or close the new spreadsheet
import clr
import System

# Add reference to Excel interop assembly
clr.AddReferenceByName("Microsoft.Office.Interop.Excel, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
import Microsoft.Office.Interop.Excel as Excel

def export_to_excel(template_file_path, new_file_path, fan_schedule_path, scheduled_equip_names, scheduled_flow, schematic_equip_names, schematic_flow, revit_equip_names, grille_inputs, level_inputs):
    try:
        # Create a new instance of Excel
        excel = Excel.ApplicationClass()
        excel.Visible = False  # Keep Excel hidden
        excel.DisplayAlerts = False  # Disable alerts (e.g., overwrite existing file)

        # Open the template workbook
        template_workbook = excel.Workbooks.Open(template_file_path)
        
        # Save the template workbook as a new workbook
        template_workbook.SaveAs(new_file_path)

        # Close the template workbook without saving changes
        template_workbook.Close(False)
        
        # Reopen the new workbook
        new_workbook = excel.Workbooks.Open(new_file_path)      
        
        # Get the first sheet of the new workbook
        new_sheet = new_workbook.Sheets(1)

        # Write the schedule paths to excel
        new_sheet.Cells[6, 2].Value2= ahu_schedule_path
        new_sheet.Cells[7, 2].Value2= fcu_schedule_path
        new_sheet.Cells[8, 2].Value2= fan_schedule_path
        new_sheet.Cells[9, 2].Value2= vav_schedule_path
        new_sheet.Cells[2, 6].Value2= date_input
        new_sheet.Cells[1, 2].Value2= project_name
        
        # Write scheduled_equip_names to Excel starting from A8
        for i, equip_name in enumerate(scheduled_equip_names):
            new_sheet.Cells[12 + i, 1].Value2 = equip_name

        # Write scheduled_flow to Excel starting from G8
        for i, flow in enumerate(scheduled_flow):
            new_sheet.Cells[12 + i, 4].Value2 = flow

        # Match schematic_equip_names to scheduled_equip_names to find index
        for i, schematic_name in enumerate(schematic_equip_names):
            if schematic_name in scheduled_equip_names:
                index = scheduled_equip_names.index(schematic_name)
                # Take the first index of schematic_flow instead of summing
                schematic_flow_value = schematic_flow[i][0]
                # Write schematic_flow_value to Excel starting from J8 using the matched index
                new_sheet.Cells[12 + index, 5].Value2 = schematic_flow_value
        
        # Match revit_equip_names to scheduled_equip_names to find index
        for i, revit_name in enumerate(revit_equip_names):
            if revit_name in scheduled_equip_names:
                index = scheduled_equip_names.index(revit_name)
                # Sum grille_inputs corresponding to index
                grille_input_sum = sum(grille_inputs[i])
                # Write grille_input_sum to Excel starting from M8 using the matched index
                new_sheet.Cells[12 + index, 6].Value2 = grille_input_sum
                
                # Write level_inputs corresponding to index in column D
                level_input = level_inputs[i][0]  # Assuming level_inputs is a list of single values
                new_sheet.Cells[12 + index, 3].Value2 = level_input
        
        # Save and close the new workbook
        new_workbook.Save()
        
        # Close the new workbook
        new_workbook.Close(True)
        
        # Quit Excel
        excel.Quit()

        # Clean up
        System.Runtime.InteropServices.Marshal.ReleaseComObject(new_sheet)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(new_workbook)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(excel)
        del new_sheet, new_workbook, excel
        
        # Release the template workbook object
        System.Runtime.InteropServices.Marshal.ReleaseComObject(template_workbook)
        del template_workbook
        
    except Exception as e:
        OUT = str(e)

# Inputs
template_file_path = IN[0]  # Provide the path to your template Excel file
new_file_path = IN[1]  # Provide the path for the new workbook
scheduled_equip_names = IN[2]  # List of scheduled equip names to export
scheduled_flow = IN[3]  # List of scheduled flow to export
schematic_equip_names = IN[4]  # List of schematic equip names to export
schematic_flow = IN[5] # List of schematic flow rates to export
revit_equip_names = IN[6]  # List of Revit equip names to export
grille_inputs = IN[7]  # List of lists of grille_input values to sum
level_inputs = IN[8]  # List of lists of level_input values
ahu_schedule_path = IN[9]  # Path to ahu schedule file
fcu_schedule_path = IN[10]  # Path to fcu schedule file
fan_schedule_path = IN[11]  # Path to fan schedule file
vav_schedule_path = IN[12]  # Path to vav schedule file
date_input = IN[13]  # Todays date
project_name = IN[14]  # Project name

# Call the export function
export_to_excel(template_file_path, new_file_path, fan_schedule_path, scheduled_equip_names, scheduled_flow, schematic_equip_names, schematic_flow, revit_equip_names, grille_inputs, level_inputs)

# Output the file path for potential downstream use
OUT = new_file_path

Project System Airflows_script.dyn (264.5 KB)

Take your function out of the try/except block to get a better idea of which step(s) fail in the function.

1 Like

Try to copy the template file then open it

replace

# Open the template workbook
template_workbook = excel.Workbooks.Open(template_file_path)

# Save the template workbook as a new workbook
template_workbook.SaveAs(new_file_path)

# Close the template workbook without saving changes
template_workbook.Close(False)

# Reopen the new workbook
new_workbook = excel.Workbooks.Open(new_file_path)

by

System.IO.File.Copy(template_file_path, new_file_path)
# Open the new workbook
new_workbook = excel.Workbooks.Open(new_file_path)

if possible, I suggest you migrate to openpyxl or Microsoft OpenXml