Is there a way to call python with xlwings without reopening the excel file?

I am calling python from Excel using xlwings. I found that when I run my macro, Excel closes and reopens to run the code. It works correctly, but it slows down. In addition, if the Excel file is not saved, a dialog box will indicate that the file is already open and that I will lose unsaved changes.

Is there a way to call python without reopening the excel file?

This is my python code (in loaddf.py):

from xlwings import Workbook, Range, Sheet def my_macro(): wb = Workbook.caller() Range('A1').value = Range('A1').value + 1 

And the VBA code in my Excel file:

 Sub loaddfsub() RunPython ("import loaddf; loaddf.my_macro()") End Sub 

Thanks for the help.

+8
python xlwings
source share
1 answer

It seems that under certain circumstances, Excel does not register the Excel workbook properly in RunningObjectTable , a prerequisite that it can be found through COM. So far, I have noticed this behavior for downloadable books from the Internet, as he first opens them in Protected View mode (depending on the settings). However, based on the reviews here, it looks like this could happen under other circumstances, possibly caused by some add-ons or security settings.

I applied a fix for this, which will be present in v0.3.1 , but you can get it right now from GitHub . Let me know if you need any help.

Update (January 16 - 2015): xlwings v0.3.1, including this fix, has just been released.

Update 2 (September 13, 2015): xlwings v0.4.0 should permanently fix this error in a reliable way.

+5
source share

All Articles