Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call python function from excel

I have written a python code, which takes 3 inputs, and return one output val. I try to write an excel function, which passes the three inputs to the python function and returns the output.

I have looked into XLwings, but there is so many issues (and the documentation is insanely poor/poorly written) thus it seems useless.

So: is there any other way to call a python function (which takes inputs) from excel?

[SOLVED (ish):] I managed, after roughly 8 hours of trying, 4 youtube videos and the xlwings homepage, to make it work.

Video for installing: https://training.zoomeranalytics.com/courses/xlwings/lectures/4231276

Video for making a function which takes input and returns output: https://www.youtube.com/watch?v=qn8xGrDuRCg&t=16s

like image 853
CutePoison Avatar asked Oct 17 '22 16:10

CutePoison


1 Answers

You could try xlOil (disclaimer: I wrote it). The docs are here, but to write a simple three input function, you would install xlOil using:

pip install xloil
xloil install

Then write:

import xloil

@xloil.func
def myfunc(x, y, z):
    return x + y * z

Put this code either:

  1. In a py file in the same directory as your spreadsheet, named SpreadsheetName.py
  2. In a py file on your python module path, then edit %APPDATA%\xlOil\xlOil.ini to load it as described in the docs.

Start Excel, open your spreadsheet and the function will be available as myfunc.

like image 94
stevecu Avatar answered Oct 30 '22 01:10

stevecu