Ekaterina  Denike
Ekaterina Denike

Reputation: 11

xlwings doesn't write data in file.xlsm when running through excel

I am trying to use xlwings for simple consolidation from several xls files. For this, I have a all_files.xlsm file that contains a button with macros assigned to it. The macros looks like:

Sub check_data()
    RunPython ("import python_code; python_code.consolidation()")
End Sub

In same folder I have a file python_code.py with function "consolidation" inside. I also use Workbook.set_mock_caller() in order to have an opportunity to run a code through python interface. It looks like:

def consolidation(file_path):
    *** smth to get the data I need ***
    ...
    *** after I got data ***
    Range('A1').table.clear_contents() #string1
    Range('A1').value = data #string2

def main():
    consolidation(file_path)

if __name__ == '__main__':
    xl.Workbook.set_mock_caller(path_to_file)
    main()

The problem is that when I am running the script through the button in excel file last two strings (string1 and string2) - Range('A1').table.clear_contents() and Range('A1').value = data doesn't work. Although the rest of the code works fine (however, it contains xlwing also). Moreover, if I run the script through the python interface using set mock caller, it works just fine, including string1 and string2 (marked in the code).

Any help and advices are really appreciated!

Upvotes: 0

Views: 737

Answers (2)

Angelo Souza
Angelo Souza

Reputation: 1

I had the same issue and in my case nothing worked until I updated Windows 365. But I also uninstalled xlwings and installed again.

Upvotes: 0

Ekaterina  Denike
Ekaterina Denike

Reputation: 11

I found decision for my problem. For using xlwings in python you should import xlwings.bas in your excel file (see manual for xlwings). It turns out, that I imported it for this macros before I've updated xlwings. So I deleted this file and imported a new one. Everything works fine now.

Upvotes: 1

Related Questions