8

I've seen from various questions on here that if an instance of Excel is opened from Python using:

xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
xl.Visible = True
wb = xl.Workbooks.Open('Test.xlsx')

Then it does not load the default add-ins. I've tried forcing my add-in to load by instead running:

xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
xl.Visible = True
addin = xl.Workbooks.Open('C:/path/addIn.xll')
wb = xl.Workbooks.Open('Test.xlsx')

However, when I do this an instance of Excel opens and I get a security message asking me to verify I want to open this add-in. If I click "Enable this add-in for this session only" Excel immediately closes and does not open my desired xlsx file.

Does anyone have any ideas how to force the add-in to load and then to allow me to open my file?

Thanks very much for your help!

4
  • Do you need to interact with Excel or just read spreadsheet data? If the latter then there are routines (that even work on Unix!) you can use to read the data from the spreadsheet (xlrd, python-excel, etc. Commented Mar 4, 2014 at 10:56
  • I need to do some calculations in Python, pass the results into Excel and then be able operate on these results using functions provided by an Excel add-in. I am quite aware this probably isn't an ideal set-up but I'm only helping out with a small of a project and am not really in a position to dictate how they're setting everything up! Commented Mar 4, 2014 at 11:09
  • Hi. I also have some xll Excel add-ins that I want to be able to load and use in Python. Did you find a way to do this? Can you share your solution? Thanks Commented Nov 8, 2019 at 14:01
  • Does my answer below not work for you? Commented Nov 9, 2019 at 17:28

3 Answers 3

7

I have actually managed to resolve this by borrowing something from this MSDN article relating to doing the same thing with VBA:

http://support.microsoft.com/default.aspx?scid=KB;en-us;q213489

The following now works perfectly:

xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
xl.Visible = True
xl.RegisterXLL('C:/path/addin.xll')
wb = xl.Workbooks.Open('Test.xlsx')
Sign up to request clarification or add additional context in comments.

1 Comment

Glad you found answer and posted it. That's funny because I came across RegisterLL (although not that particular page you reference, good find) but it's the one option that I didn't have enough info on to determine if it was relevant.
5

I had the same problem, but couldn't use xl.RegisterXLL('C:/path/addin.xla') from the accepted answer, because it only works with .XLL files, and I had a .XLA file.

Instead, I found that this worked:

xl = win32com.client.gencache.EnsureDispatch('Excel.Application')

# Need to load the addins before opening the workbook
addin_path = r'C:\path\addin.xla'
xl.Workbooks.Open(addin_path)
xl.AddIns.Add(addin_path).Installed = True

wb = xl.Workbooks.Open(r"C:\my_workbook.xlsm")

Comments

1

I have had much better success using Excel via win32com than any of the other methods, but you might want to look at pyxll (https://www.pyxll.com/introduction.html). Here are a few other things:

  • Have you checked that addin.Installed == True?
  • Have you tried AddIns.Add("c:\windows\addins\TSXL\TSXL.xll").Installed = True?
  • Try xl.DisplayAlerts=False before opening the workbooks
  • Have you tried the four steps in last answer of Automating Excel via COM/Python - standard addins won't load at startup, I copy them here:

    1. Open the XLA / XLL file representing the addin in question
    2. Set addins(addin_name).Installed = False
    3. Addins(addin_name).Add(addin_file_path)
    4. Set addins(addin_name).Installed = True

3 Comments

Thanks for your response Schollii!
@Ben Np. I'd be curious to hear if you have tried any of the above.
I didn't get the chance (I stumbled upon the other solution in between posting the question and seeing your response). If I get time I will look into whether they too solve the problem or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.