Get the App
SLTechnology News&Howtos  ›  Development  › 

How to call Python code directly in Excel

Shulou Source: shulou.com Published: 2022-06-03 07:25:01 11月15日 Update

How to call Python code directly in Excel, I believe many inexperienced people are helpless about this, for this reason this article summarizes the causes and solutions of the problem, through this article I hope you can solve this problem.

I accidentally saw a question on Zhihu asking if I could call Python code in Excel:

I found it interesting, so I checked the information and found that Python's xlwings module can be inserted into Excel and run like VBA. This is a gospel for the majority of Python enthusiasts.

Here is a detailed introduction to how to call python code in excel

Step 1: Load xlwings module in excel

For the use of wlxings, we have already sent it before, specifically refer to python automation office| Batch Excel file sharp-xlwings, here no longer repeated

Start with the xlwings module as a tab on the excel ribbon

Click File-Options to open the following dialog box:

Click Trust Center-Trust Center Settings = Macro Settings, check Trust Access to VBA Project Object Model:

Click "Development Tools-Add-ons" in the function area and check Xlwings:

If there is no Xlwings, click "Browse" and select the xlwings.xlam file under the xwings installation directory. Loading it will be OK.

At this time, you can see the xlwings module in the Excel function area

Step 2: Import and invoke python code

Use quickstart of Xlwings module to quickly create working Pu and python code files with macros, as follows.

WIN+R Enter cmd to open the command line, use cd command to switch to the path you want, and enter the command "xlwings quickstart table" under the path:

If the above message appears, it means that the file was successfully created. The following files can be found in the folder:

Open the table.py file and you can see the automatically generated python code:

There are main() and hello() functions inside, which can realize the corresponding functions.

We open the table.xlsm file, click on the function area "xlwings-Import Functions" in turn, wait for the load to complete, and enter hello() function in cell A1:

Press Enter:

This allows you to call Python custom functions in Excel.

Step 3: Inference

Let's define a function even_to_odd, which can change even numbers to odd numbers, as follows:

@xw.func def even_to_odd(x): if x%2==0: x=x-1 return x

Put it into table.py file and run it. Click "xlwings-Import Functions" in the functional area in turn. After loading, enter even_to_odd() function as follows:

After reading the above, do you know how to call Python code directly in Excel? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

Tags: File code function function ribbon module input command problem content method more path run success inference helplessness for this interesting even number Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL Apple Huawei Linux