Search code examples
python-3.xxlwings

xlwings set_mock_caller() return None


Windows 10, Python 3.6, xlwings 0.27.8

When trying to debug my code outside of RunPython, I keep stumbling on the following issue, for example:

import xlwings as xw
xlsx_file = 'anExcelFile.xlsm'
xlwx = xw.book(xlsx_file).set_mock_caller()

From there, I am hoping to be able to use xlsw as normally as such if I had used the routine from RunPython, but now, typing xlsw returns None

However, if i do: xlsx = xw.book(xlsx_file).set_mock_caller

then xlsx contains: <Book ['anExcelFile.xlsm]> but still, xlsx() returns None.

Any lead on what I am getting wrong would help, thanks!


Solution

  • Finally understood my issue. As explained in xlwings, the code sequence should read as follows:

    import xlwings as xw
    xlsx_file = 'anExcelFile.xlsm'
    xw.book(xlsx_file).set_mock_caller()  
    # Sets the Excel file which is used to mock xw.Book.caller() 
    # when the code is called from Python and not from Excel via RunPython.
    # and then: 
    xlsx = xw.Book.caller()  
    

    Now, xlsx returns: <Book [anExcelFile.xlsm]>