Search code examples
pythonexcelpandasxlwings

xlwings udf function erase next cell on excel


I have a little problem on excel using xlwings and i really don't know how to fix it. When i'm using an UDF function that return for example a panda dataframe, let suppose that my dataframe is 3 colums width (no necessary condition on rows), then on the 4th columns in excel, if i write some datas on it, my panda dataframe will erase it as soon as i calculate the sheet... Although the dataframe is not using this column at all while it's 3 columns large and not 4 ... I don't know if i'm clear enough. Let me know !

thank you very much in advance.

@xw.func
@xw.ret(expand='table') 
def hello(nb):
    nb = int(nb)
    return [["hello","you"] for i in range(nb)]

before recalculate the sheet

after recalculate the sheet


Solution

  • It seems that in the documentation of xlwings, it is necessary to have an empty row and column at the bottom and to the right. if not it will overwrite it

    http://docs.xlwings.org/en/stable/api.html#xlwings.xlwings.ret