Home>Article>Backend Development> How to use python in excel?
How to apply python in excel: First [return sorted(s)] create a directory locally, save this code and name it [Methods.py]; then open Excel and fill in some data to provide python script processing; finally call the python script.
How to apply python in excel:
The python code to be called in Excel:
def merge_sort_unique(lists): s = set() for L in lists: s.update(L)
return sorted(s)
Create a directory locally, save this code and name it Methods.py. Let's see step by step how to call this script from an Excel worksheet. Open Excel, fill in some data to provide python script processing, and keep this form in the same directory as the python script just now. Your worksheet should look like this:
Next, open VBA and submit to ExcelPython.
Open VBA and pressAlt F11
Then enter the References of the tool interface... If you have installed ExcelPython correctly, you can select it from the dialog box.
Now, prepare to call the python script, create a new module (Insert | Module) and type the following VBA code:
Function msu(lists As Range) Set methods = PyModule("Methods", AddPath:=ThisWorkbook.Path) Set result = PyCall(methods, "merge_sort_unique", PyTuple(lists.Value2)) msu = WorksheetFunction.Transpose(PyVar(result)) ExitFunction EndFunction
Related learning Recommended:python video tutorial
The above is the detailed content of How to use python in excel?. For more information, please follow other related articles on the PHP Chinese website!