Home>Article>Backend Development> How to use python in excel?

How to use python in excel?

coldplay.xixi
coldplay.xixi Original
2020-07-03 11:51:08 8968browse

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 use python in excel?

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:

How to use python in excel?

Next, open VBA and submit to ExcelPython.

Open VBA and pressAlt F11Then enter the References of the tool interface... If you have installed ExcelPython correctly, you can select it from the dialog box.

How to use python in excel?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What can pycharm do? Next article:What can pycharm do?