Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call Python code directly in Excel

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report