


How to call Python scripts in Excel to automate data processing!
Speaking of Excel, it is definitely the king in the field of data processing. Although it has been born for more than 30 years, there are still 750 million loyal users around the world, and as an Internet celebrity language Python has only 7 million developers.
Excel is the most popular programming language in the world. Yes, you read that right. Since Microsoft introduced the LAMBDA definition function, Excel has been able to implement algorithms in programming languages, so it is Turing complete, just like JavaScript, Java, and Python.
Although Excel is an indispensable tool for small-scale data scenarios, it will be somewhat inadequate when facing big data.
We know that an Excel table can display up to 1,048,576 rows and 16,384 columns. Processing a table with hundreds of thousands of rows may cause some lag. Of course, you can use VBA for data processing, or you can use Python. to operate Excel.
This is the topic of this article, Python’s third-party library-xlwings, which serves as an interactive tool between Python and Excel, allowing you to easily call Python scripts through VBA to achieve complex data analysis.
For example, automatically import data:
or randomly match text:
1. Why integrate Python with Excel VBA?
VBA, as the built-in macro language of Excel, can do almost anything, including automation, data processing, analysis and modeling, etc. So why use Python to integrate Excel VBA? There are three main reasons:
- If you are not proficient in VBA, you can directly use Python to write analysis functions for Excel calculations, and No need to use VBA;
- Python runs faster than VBA, and code writing is more concise and flexible;
- There are many excellent third-party libraries in Python, which can be used at any time, which can save a lot of coding time;
For Python enthusiasts, come You may already be very familiar with data science libraries such as pandas and numpy. If you can use them for Excel data analysis, it will be even more powerful.
2. Why use xlwings?
There are many libraries in Python that can operate Excel, such as xlsxwriter, openpyxl, pandas, xlwings, etc.
But compared to other libraries, xlwings has almost the best overall performance, and xlwings can call Python code through Excel macros.
The picture comes from Early Python
I won’t explain much about the introductory use of xlwings here.
Installing xlwings is very simple. Quick installation can be achieved through pip on the command line:
pip install python
After installing xlwings, you need to install the Excel integration plug-in of xlwings. Before installation, you need to close all Excel applications. Otherwise, an error will be reported.
Also enter the following command on the command line:
xlwings addin install
The following prompt indicates that the integrated plug-in is installed successfully.
After xlwings and the plug-in are installed, open Excel at this time and you will find an xlwings menu box appearing on the toolbar, which means that the xlwings plug-in is successfully installed. It serves as a bridge for VBA calls. Python script for matchmaking.
In addition, if your menu bar does not yet display "Development Tools", you need to add "Development Tools" to the ribbon because we need to use macros.
The steps are simple:
1. On the "File" tab, go to "Customize > Options".
2. Under "Customize Ribbon" and "Main Tab", select the "Development Tools" checkbox.
The menu bar displays the development tools, and you can start using macros.
If you still don’t know what a macro is, you can temporarily understand it as a tool for automation and batch processing.
At this point, the preliminary preparation work is completed, and the next step is actual combat!
三、玩转xlwings
要想在excel中调用python脚本,需要写VBA程序来实现,但对于不懂VBA的小伙伴来说就是个麻烦事。
但xlwings解决了这个问题,不需要你写VBA代码就能直接在excel中调用python脚本,并将结果输出到excel表中。
xlwings会帮助你创建.xlsm
和.py
两个文件,在.py
文件里写python代码,在.xlsm
文件里点击执行,就完成了excel与python的交互。
怎么创建这两个文件呢?非常简单,直接在命令行输入以下代码即可:
xlwings quickstart ProjectName
这里的ProjectName
可以自定义,是创建后文件的名字。
如果你想把文件创建到指定文件夹里,需要提前将命令行导航到指定目录。
创建好后,在指定文件夹里会出现两个文件,就是之前说的.xlsm
和.py
文件。
我们打开.xlsm
文件,这是一个excel宏文件,xlwings已经提前帮你写好了调用Python的VBA代码。
按快捷键Alt + F11
,就能调出VBA编辑器。
Sub SampleCall()<br>mymodule = Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".", -1, vbTextCompare) - 1))<br>RunPython "import " & mymodule & ";" & mymodule & ".main()"<br>End Sub<br><br>
里面这串代码主要执行两个步骤:
1、在.xlsm
文件相同位置查找相同名称的.py
文件
2、调用.py
脚本里的main()
函数
我们先来看一个简单的例子,自动在excel表里输入['a','b','c','d','e']
第一步:我们把.py
文件里的代码改成以下形式。
import xlwings as xw import pandas as pd def main(): wb = xw.Book.caller() values = ['a','b','c','d','e'] wb.sheets[0].range('A1').value = values @xw.func def hello(name): return f"Hello {name}!" if __name__ == "__main__": xw.Book("PythonExcelTest.xlsm").set_mock_caller() main()
然后在.xlsm
文件sheet1
中创建一个按钮,并设置默认的宏,变成一个触发按钮。
设置好触发按钮后,我们直接点击它,就会发现第一行出现了['a','b','c','d','e']
。
同样的,我们可以把鸢尾花数据集自动导入到excel中,只需要在.py文件里改动代码即可,代码如下:
import xlwings as xw import pandas as pd def main(): wb = xw.Book.caller() df = pd.read_csv(r"E:\test\PythonExcelTest\iris.csv") df['total_length'] = df['sepal_length'] + df['petal_length'] wb.sheets[0].range('A1').value = df @xw.func def hello(name): return f"Hello {name}!" if __name__ == "__main__": xw.Book("PythonExcelTest.xlsm").set_mock_caller() main()
好了,这就是在excel中调用Python脚本的全过程,你可以试试其他有趣的玩法,比如实现机器学习算法、文本清洗、数据匹配、自动化报告等等。
Excel+Python,简直法力无边。
参考medium文章
The above is the detailed content of How to call Python scripts in Excel to automate data processing!. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
