Skip to Content

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.

Hello Function

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 TypeExcel ExamplePython TypePython Example
Number42float42 (even whole numbers become floats)
Number3.14float3.14
String”hello”str”hello”
BooleanTRUEboolTrue
Array{1, 2, 3}2D List[[1, 2, 3]]
ArrayA1:B22D List[[1, 2], [3, 4]]
NullReference to an empty cellNoneNone
NullUnset optional LAMBDA argumentNoneNone
NullArg skipped with ,NoneNone
Date45678 (serial format)float45678*

*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 TypePython ExampleExcel TypeExcel Example
int42Number42
float3.14Number3.14
str”hello”String”hello”
boolTrueBooleanTRUE
2D list (Matrix)[[1, 2], [3, 4]]ArrayA1:B2*
2D list (Column Vector)[[1], [2], [3]]ArrayA1:A3*
2D list (Row Vector)[[1, 2, 3]]ArrayA1:C1*
NoneNoneNullEmpty cell
datetimedatetime(2022, 1, 1)Date44519
tuple(1, 2, 3)ArrayA1: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!`?

- `#VALUE!`: An argument to a function is missing or it is the wrong type (e.g. incorrect range reference). - `#NAME?`: The function name is spelled incorrectly, e.g. `BOARDFLARE.RUNPYY`. - `#BUSY!`: This is normal for 5-10 seconds if you are importing libraries for the first time and have a slow internet connection.

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?

You should be able to access any public API that supports [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS).

Can I build a LAMBDA function using the native Excel PY?

No, that is the original reason we built this add-in. At this time the Excel PY function does not allow you to call it from a formula.

Can I access local files on my machine?

No. The Pyodide runtime runs in a [Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) in the browser which does not have access to your local machine. This is a browser security feature.

Where is `stdout` or `stderr` is displayed?

Any output to `stdout` or `stderr` is displayed in the `Output` tab of the add-in task pane, but it is only returned at the completion of the execution of your code.

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.
Last updated on