This library perfectly combines Python with Excel

e76416ca3e27a09b08d993747c922456.png

Lost little book boy

Needed after reading

5

minute

Speed ​​reading only takes 2 minutes

1

   

Introduction

Both Python and Excel play a very important role in modern data analysis and processing. It would be an exciting idea to be able to seamlessly combine the two and play to their respective strengths. Fortunately, the emergence of PyXLL, an Excel plug-in, makes it possible to realize the above idea. PyXLL allows us as developers to perfectly integrate the powerful functions of Python with our familiar Excel interface, thus injecting unlimited possibilities into Excel.

2

   

PyXLL installation and environment setup

To start using PyXLL, you first need to install it and set up your environment.

Install PyXLL using pip

pip install pyxll 
pyxll install

During the installation process, follow the prompts to make selections. The PyXLL plug-in can be installed online or downloaded at: https://www.pyxll.com/download.html ( https://www.pyxll.com/download.html ) , for the sake of simplicity, just download and install it through the script.

c7358990ab45682a2350e975892ea692.jpeg

After the installation is complete, you can view it through the command pyxll status

8b09ee63dffb023f5759fae2664fb892.jpeg

You can modify the configuration file through the command pyxll configure. After execution, the configuration file pyxll.cfg will be automatically opened.

fc0b12d2d808b9c6f3bf668761312bba.jpeg

3

   

Instructions

In PyXLL, we can easily create Python functions and expose them to Excel for use.

Create a new Python module (such as test.py, for simplicity, store the file in the example directory under the PyXLL installation path, this directory is already included in the PYTHONPATH variable in the pyxll.cfg file), and define a Python function fib, and then use the @xl_func decorator to expose the function to Excel.

from pyxll import xl_func


@xl_func
def fib(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    return fib(n-1) + fib(n-2)

Next, you need to edit the configuration file pyxll.cfg and add the module name test to the modules variable.

03ac16a91dbde2618177cac8cf020919.jpeg

Then, create a new Excel file and open it

b30342b6a9aad09b50735f07416d1d3f.jpeg

Here we can apply for free for 30 days, click Start Trial to enter

Select the Add-ins menu and then select PyXLL -> Reload PyXLL to reload the configured Python module so that new functions can be discovered by Excel.

4aa130ab4c980f347588e9a8a511d432.jpeg

Now, you can use your Python function in Excel formulas. In a worksheet cell, use your function just like any other Excel function. For example, execute =fib(10) in cell A1

78554caca19e53ebd5634063193700a4.jpeg

After pressing Enter, cell A1 is filled with the value 55.

3f5cd8445ae1a50a2d153e35ff7a0ff1.jpeg

4

   

Summarize

PyXLL is more than just a tool, it represents a new way of thinking about combining Python and Excel. It enables data analysts, developers, and users to more efficiently utilize the power of Python and easily handle complex data and computing tasks. Whether for experienced Python developers or users who have never been exposed to Python before, PyXLL is a powerful and easy-to-use tool that greatly unleashes the potential of Excel.

5

   

References

  • https://www.pyxll.com/ ( https://www.pyxll.com/ )

6

   

free community

1312c115800a346202e5e9725a459498.jpeg

5afd2901620f99c0e360a682affc66d7.gif

Guess you like

Origin blog.csdn.net/djstavaV/article/details/133004440