Microsoft has added Python directly into Excel, which means you can write Python code in a spreadsheet cell and have it run against your data — without installing anything, setting up an environment, or leaving Excel. It’s one of the most significant additions to Excel in years, and this guide explains what it is, how to use it, and whether it’s the right tool for you.
What Python in Excel Is
Python in Excel lets you use the =PY() function to run Python code from inside an Excel cell. When you enter a Python cell, Excel sends your code to Microsoft’s cloud servers, runs it in a secured Anaconda environment, and returns the result — which can be a value, a table, or even a chart displayed inside the cell.
This means you have access to Python libraries like pandas (for data manipulation), matplotlib and seaborn (for visualisation), scikit-learn (for machine learning), and statsmodels (for statistical analysis) — all from within a familiar Excel interface.
The Python runtime is provided by Anaconda, hosted on Microsoft’s infrastructure. Your data and code are processed in the cloud, not on your local machine.
Who Can Use Python in Excel
Python in Excel is available to Microsoft 365 subscribers on Windows. It began rolling out in 2023 and is now available to most Microsoft 365 Personal, Family, and business subscribers. Mac support was added later.
How to check if you have it
- Open Excel.
- Click the Formulas tab on the ribbon.
- Look for a Python group or button. If it’s there, you have access.
- Alternatively, type
=PY(in any cell. If Excel recognises the function, it’s enabled.
If you don’t see it, make sure your Microsoft 365 is fully updated (File > Account > Update Options > Update Now).
Inserting a Python Cell
To run Python code in Excel:
- Click a cell where you want the output to appear.
- Go to Formulas > Python and click Insert Python, or type
=PY(directly in the cell. - The cell turns green to show it’s in Python mode.
- Type your Python code.
- Press Ctrl+Enter to run the code (not just Enter — that would move to the next cell).
The result appears in the cell. If it’s a DataFrame (a table), you’ll see a small icon you can click to expand it across the sheet.
Reading Excel Data into Python
To work with data from your spreadsheet in Python, use the xl() function to reference a range:
df = xl("A1:D100", headers=True)
This converts your Excel range into a pandas DataFrame called df, which you can then manipulate with standard pandas commands. The headers=True argument tells Excel that the first row contains column names.
Basic Examples to Try
Calculate summary statistics
df = xl("A1:D100", headers=True)
df.describe()
This returns a summary table showing count, mean, standard deviation, min, max and quartiles for every numeric column in your range.
Filter rows based on a condition
df = xl("A1:D100", headers=True)
df[df["Revenue"] > 1000]
Returns only the rows where the Revenue column is greater than 1000.
Create a matplotlib chart in a cell
import matplotlib.pyplot as plt
df = xl("A1:B13", headers=True)
fig, ax = plt.subplots()
ax.bar(df["Month"], df["Sales"])
ax.set_title("Monthly Sales")
fig
This creates a bar chart directly inside an Excel cell. You can resize it like any other Excel object.
Group and summarise data
df = xl("A1:C200", headers=True)
df.groupby("Region")["Revenue"].sum()
Returns total revenue grouped by region — the equivalent of a pivot table, but in Python.
Using Python for Things Excel Formulas Can’t Do Easily
Complex text processing
Python is far more capable than Excel formulas for parsing, cleaning, and transforming text. For example, extracting specific patterns from free-text fields, parsing JSON data stored in cells, or matching names using fuzzy matching libraries.
Machine learning predictions
With scikit-learn available, you can train a simple predictive model on your Excel data and return predictions as a column in your spreadsheet — all without leaving Excel.
Statistical analysis
Regression analysis, correlation matrices, hypothesis testing — these are all available through pandas and statsmodels, without needing a separate tool like R or a Jupyter notebook.
Working with dates and time series
Pandas handles time series data much more gracefully than Excel, particularly for resampling, rolling averages, and irregular date intervals.
Important Limitations
Runs in Microsoft’s cloud, not on your computer
Your code and data are sent to Microsoft’s Azure cloud to be executed. This means Python in Excel requires an internet connection, and it may not be suitable for sensitive or confidential data depending on your organisation’s policies.
No internet access from Python code
The Python environment is sandboxed — your code cannot make external API calls or access websites. If you want to pull live data from an API, you’ll need to do that outside Excel and import the data manually.
Limited library selection
You can only use libraries that are included in the Anaconda distribution provided by Microsoft. You cannot install your own libraries. The available libraries are extensive and cover most data analysis needs, but custom packages won’t work.
Performance on large datasets
Because processing happens in the cloud, there can be a noticeable delay when running Python on large datasets. For very large files, consider pre-filtering your data in Excel before passing it to Python.
Who Python in Excel Is Really For
Python in Excel is genuinely useful for people who already know Python and want to use it without switching tools. If you’re comfortable writing pandas code and you work primarily in Excel, it removes a significant amount of friction.
It is not a beginner feature. If you don’t already know Python, the learning curve is steep and you’ll get faster results using Copilot or ChatGPT for data analysis. Python in Excel assumes you understand Python syntax, DataFrame operations, and at least basic data science concepts.
For home users or small business owners who don’t have a Python background, tools like Microsoft Copilot or ChatGPT will answer most data questions more quickly and with far less setup. Python in Excel is best thought of as a power tool for analysts who want to bring their existing Python skills into the spreadsheet environment.