Python for Excel
Overview
Install the add-in . Create an Excel function from a Python function in two easy steps as follows:
1. Write a Python function
Using the add-in code editor, write or paste your function code, e.g.:
def hello(name):
""" Returns a greeting. """
return f"Hello {name}!"
2. Run as an Excel function
Python code is stored in workbook settings, and the function is added to the name manager as follows:
=LAMBDA(name, BOARDFLARE.EXEC("hello", name))
The function is now available for use in the workbook as follows:
=HELLO(name)
How it works
When you save this code, the Python function name hello
and argument name
are parsed to create a LAMBDA function =LAMBDA(name, BOARDFLARE.EXEC("hello", name))
which is saved to the name manager with the name HELLO
. The Python code for the function named hello
is saved to the workbook settings so it is embedded in the workbook. The first line of the Python function docstring is also added as a comment to the name manager to provide an autocomplete description.
Each time the function is invoked, BOARDFLARE.EXEC
loads the code from the workbook settings and runs it with the argument. Since the code and LAMBDA are both stored in the workbook, anyone who uses the workbook can use the function. If they don’t have the add-in installed, Excel will automatically prompt them to do so.
Features
Some key features of the add-in are as follows:
✅ Use Python in formulas and LAMBDA functions.
🆓 Free for unlimited use.
🌐 Works in Excel for web as well as desktop.
☁️ Runtime has network access for API calls (needs CORS).
📦 Import custom packages (pure Python only).
🔒 Code is stored in your workbook and runs locally.
Limitations
- Your Python code must be a function. All data is passed to the function as arguments.
- Excel PY range references, e.g.
xl("Sheet1!A1:A10")
, cannot be used to pass data to the code. - Local files cannot be accessed due to browser security restrictions.
*args
and**kwargs
are not supported because LAMBDA functions do not support repeating arguments.
Importing Packages
Pyodide is the Python runtime used by the add-in, and includes a number of built-in packages . Any imports of built-in packages (e.g. import pandas as pd
) will work without any additional installation required. However, you can only use the version of the package that is included in the Pyodide runtime, which may not be the latest version.
You may also import any pure Python (OS-independent ) packages hosted on PyPI using micropip
as follows:
import micropip
await micropip.install(['azure-ai-textanalytics', 'textdistance'])
# Continue with your code...
Note: Prior to version 1.4.0, packages were automatically installed from PyPI, but this only worked if the package name was the same as the import name, which is not always the case. To avoid this confusion, you must now explicitly install packages using micropip
as shown above.
Type Conversion
The following type conversions will take place on the arguments passed to your function from Excel:
Excel Type | Excel Example | Python Type | Python Example |
---|---|---|---|
Number | 42 | float | 42 (even whole numbers become floats) |
Number | 3.14 | float | 3.14 |
String | ”hello” | str | ”hello” |
Boolean | TRUE | bool | True |
Array | {1, 2, 3} | 2D List | [[1, 2, 3]] |
Array | A1:B2 | 2D List | [[1, 2], [3, 4]] |
Null | Reference to an empty cell | None | None |
Null | Unset optional LAMBDA argument | None | None |
Null | Arg skipped with , | None | None |
Date | 45678 (serial format) | float | 45678* |
*Excel stores dates as numbers in serial format, and since we have no way to distinguish these from non-date numbers, they are not converted to datetime objects, and simply become a Python float. If you want to convert an Excel serial to a Python datetime
, you can use the following code:
from datetime import datetime, timedelta
def excel_date_to_datetime(serial):
return datetime(1899, 12, 30) + timedelta(days=serial)
The value returned by your Python function will similarly be converted to the corresponding Excel type as follows:
Python Type | Python Example | Excel Type | Excel Example |
---|---|---|---|
int | 42 | Number | 42 |
float | 3.14 | Number | 3.14 |
str | ”hello” | String | ”hello” |
bool | True | Boolean | TRUE |
2D list (Matrix) | [[1, 2], [3, 4]] | Array | A1:B2* |
2D list (Column Vector) | [[1], [2], [3]] | Array | A1:A3* |
2D list (Row Vector) | [[1, 2, 3]] | Array | A1:C1* |
None | None | Null | Empty cell |
datetime | datetime(2022, 1, 1) | Date | 44519 |
tuple | (1, 2, 3) | Array | A1:A3 |
*Assumes formula is in cell A1.
If your function returns another Python type such as a list, set, or other non-scalar types, an error will be thrown. We have support for converting numpy and pandas types to help reduce errors, but recommend you not rely on these.
Example Functions
We have a growing collection of example functions that you can use as a starting point for your own code. You can find these in the Functions section of the website.
FAQ
Why am I getting Excel errors like `#VALUE!`, `#NAME?`, `#BUSY!`?
To the left of the bottom of the task pane, you may also see additional errors such as the following:
Error loading add-ins
We're starting the add-ins runtime, just a moment...
Sometimes Excel will throw various errors when the custom function is not properly initialized, in which case you can try restarting Excel, or reloading the browser window.
Can Python code access the network?
Can I build a LAMBDA function using the native Excel PY?
Can I access local files on my machine?
Where is `stdout` or `stderr` is displayed?
Changelog
1.4.0 - 2025-06-20
- Pyodide updated to v0.27.5, which updates Python to 3.13.2 and many built-in packages such as pandas.
- Imports for any package not in built-in packages must now be explicitly imported using micropip.
- Insert result, not formula option in function dialog for cases where you want a static result.
- Charts: if a function returns an image data URL, insert result will insert image.
- Python function names can now be mixed case.
- OneDrive: Permissions are now scoped to AppFolder only; added support Microsoft personal accounts.