Prashanth Balakrishna
Prashanth Balakrishna

Reputation: 53

How do I get an XLA add in into excel through python using win32com.client?

I have to run macros in an excel file, which can be done through python win32. But I have a third party excel add-in required to run the macros. The excel which is opened through win32 does not have the add-ins linked. The add-in is in a XLA file format.

This is my code

new_file_path = path to excel_file.xlsm
xl = win32com.client.Dispatch('Excel.Application')
xl.Visible = True
refprop_path = path to XLA file
xl.Workbooks.Open(refprop_path)
xl.AddIns.Add(refprop_path)

xl.Workbooks.Open(new_file_path)
xl.Application.Run("iter1.xlsm!Sheet1.copy_data")
for i in range(0, 3):
    xl.Application.Run("iter1.xlsm!Sheet1.temp_const_gauge")
    xl.Application.Run("iter1.xlsm!Sheet1.copy_data")
xl.Application.Save()
xl.Application.Quit()

The add-in as not added and I get an error.

xl.AddIns.Add(refprop_path).Installed = True
  File "<COMObject <unknown>>", line 3, in Add
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u'Add method of AddIns class failed', u'xlmain11.chm', 0, -2146827284), None)

Upvotes: 2

Views: 2381

Answers (1)

Prashanth Balakrishna
Prashanth Balakrishna

Reputation: 53

I did not have to explicitly write a line to add the add in. I removed that line and the problem was solved.

Working code:

new_file_path = path to excel_file.xlsm
xl = win32com.client.Dispatch('Excel.Application')
xl.Visible = True
refprop_path = path to XLA file
xl.Workbooks.Open(refprop_path)

xl.Workbooks.Open(new_file_path)
xl.Application.Run("iter1.xlsm!Sheet1.copy_data")
for i in range(0, 3):
    xl.Application.Run("iter1.xlsm!Sheet1.temp_const_gauge")
    xl.Application.Run("iter1.xlsm!Sheet1.copy_data")
xl.Application.Save()
xl.Application.Quit()

I seems that the add-in in the XLA file can be used just by keeping it open in the background.

Upvotes: 2

Related Questions