Read Excel file node Opens Excel file

Hi @leonard.moelders ,
I want to use the same code with CPyton3 but i get a warning that the object has no attribute Close.
Do you have an idea how to solve it?

Hi @Mohamad_Kayali ,

here a solution

import sys
import clr
import System 
from System.Runtime.InteropServices import Marshal

clr.AddReference('Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c')
from Microsoft.Office.Interop import Excel
from System.Runtime.InteropServices import Marshal

def excel_close():
    xlApp = Excel.Application(Marshal.GetActiveObject("Excel.Application"))
    allWorkbooks = Excel.Workbooks(xlApp.get_Workbooks())
    lst_WkbsName = [Excel.Workbook(x).get_Name() for x in allWorkbooks]
    for wb in allWorkbooks:
        wb = Excel.Workbook(wb)
        wb.Close()
    xlApp.Quit()

excel_close()

Please note that this solution Marshal.GetActiveObject() will not work with Revit 2025+ (.Net Core)
Alternative solutions:

  • use a python module as comtypes.
  • Kill Excel Process
  • build or use a C# library
  • use Marshal.BindToMoniker (if you know the Excel path)
1 Like

I’m getting another warning ( Workbook has no attribute get_Name)

With which version of Revit ? 2025 ?

try to comment or remove this line
lst_WkbsName = [Excel.Workbook(x).get_Name() for x in allWorkbooks]

1 Like

2023, it worked after commenting the lined you have mentioned. Thank you :slight_smile:

1 Like