Table of Contents
1. Why integrate Python with Excel VBA? " > 1. Why integrate Python with Excel VBA?
2. Why use xlwings? " > 2. Why use xlwings?
三、玩转xlwings" >三、玩转xlwings
Home Backend Development Python Tutorial How to call Python scripts in Excel to automate data processing!

How to call Python scripts in Excel to automate data processing!

Apr 13, 2023 am 09:19 AM
excel python

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: How to call Python scripts in Excel to automate data processing!

or randomly match text: How to call Python scripts in Excel to automate data processing!

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:


  1. 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;

    1. Python runs faster than VBA, and code writing is more concise and flexible;

    1. 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.

    How to call Python scripts in Excel to automate data processing!The picture comes from Early PythonHow to call Python scripts in Excel to automate data processing!

    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
    
    Copy after login

    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
    
    Copy after login

    The following prompt indicates that the integrated plug-in is installed successfully. How to call Python scripts in Excel to automate data processing!

    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.

    How to call Python scripts in Excel to automate data processing!

    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.

    How to call Python scripts in Excel to automate data processing!

    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
    
    Copy after login

    这里的​​ProjectName​​可以自定义,是创建后文件的名字。

    How to call Python scripts in Excel to automate data processing!

    如果你想把文件创建到指定文件夹里,需要提前将命令行导航到指定目录。

    创建好后,在指定文件夹里会出现两个文件,就是之前说的​​.xlsm​​​和​​.py​​文件。

    How to call Python scripts in Excel to automate data processing!

    我们打开​​.xlsm​​文件,这是一个excel宏文件,xlwings已经提前帮你写好了调用Python的VBA代码。

    按快捷键​​Alt + F11​​​,就能调出VBA编辑器。

    How to call Python scripts in Excel to automate data processing!


    Sub SampleCall()<br>mymodule = Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".", -1, vbTextCompare) - 1))<br>RunPython "import " & mymodule & ";" & mymodule & ".main()"<br>End Sub<br><br>
    Copy after login

    里面这串代码主要执行两个步骤:

    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()
    
    
    
    Copy after login

    然后在​​.xlsm​​​文件​​sheet1​​中创建一个按钮,并设置默认的宏,变成一个触发按钮。

    How to call Python scripts in Excel to automate data processing!How to call Python scripts in Excel to automate data processing!How to call Python scripts in Excel to automate data processing!

    设置好触发按钮后,我们直接点击它,就会发现第一行出现了​​['a','b','c','d','e']​​。

    How to call Python scripts in Excel to automate data processing!

    同样的,我们可以把鸢尾花数据集自动导入到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()
    
    
    
    Copy after login

    How to call Python scripts in Excel to automate data processing!

    好了,这就是在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!

    Statement of this Website
    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

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    What is the function of C language sum? What is the function of C language sum? Apr 03, 2025 pm 02:21 PM

    There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

    Is distinctIdistinguish related? Is distinctIdistinguish related? Apr 03, 2025 pm 10:30 PM

    Although distinct and distinct are related to distinction, they are used differently: distinct (adjective) describes the uniqueness of things themselves and is used to emphasize differences between things; distinct (verb) represents the distinction behavior or ability, and is used to describe the discrimination process. In programming, distinct is often used to represent the uniqueness of elements in a collection, such as deduplication operations; distinct is reflected in the design of algorithms or functions, such as distinguishing odd and even numbers. When optimizing, the distinct operation should select the appropriate algorithm and data structure, while the distinct operation should optimize the distinction between logical efficiency and pay attention to writing clear and readable code.

    Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

    There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

    How to understand !x in C? How to understand !x in C? Apr 03, 2025 pm 02:33 PM

    !x Understanding !x is a logical non-operator in C language. It booleans the value of x, that is, true changes to false, false changes to true. But be aware that truth and falsehood in C are represented by numerical values ​​rather than boolean types, non-zero is regarded as true, and only 0 is regarded as false. Therefore, !x deals with negative numbers the same as positive numbers and is considered true.

    What does sum mean in C language? What does sum mean in C language? Apr 03, 2025 pm 02:36 PM

    There is no built-in sum function in C for sum, but it can be implemented by: using a loop to accumulate elements one by one; using a pointer to access and accumulate elements one by one; for large data volumes, consider parallel calculations.

    Does H5 page production require continuous maintenance? Does H5 page production require continuous maintenance? Apr 05, 2025 pm 11:27 PM

    The H5 page needs to be maintained continuously, because of factors such as code vulnerabilities, browser compatibility, performance optimization, security updates and user experience improvements. Effective maintenance methods include establishing a complete testing system, using version control tools, regularly monitoring page performance, collecting user feedback and formulating maintenance plans.

    Copy and paste Love code Copy and paste Love code for free Copy and paste Love code Copy and paste Love code for free Apr 04, 2025 am 06:48 AM

    Copying and pasting the code is not impossible, but it should be treated with caution. Dependencies such as environment, libraries, versions, etc. in the code may not match the current project, resulting in errors or unpredictable results. Be sure to ensure the context is consistent, including file paths, dependent libraries, and Python versions. Additionally, when copying and pasting the code for a specific library, you may need to install the library and its dependencies. Common errors include path errors, version conflicts, and inconsistent code styles. Performance optimization needs to be redesigned or refactored according to the original purpose and constraints of the code. It is crucial to understand and debug copied code, and do not copy and paste blindly.

    How to obtain real-time application and viewer data on the 58.com work page? How to obtain real-time application and viewer data on the 58.com work page? Apr 05, 2025 am 08:06 AM

    How to obtain dynamic data of 58.com work page while crawling? When crawling a work page of 58.com using crawler tools, you may encounter this...

    See all articles