Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
431 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
|
Calling Python code from within Excel, and the vast functionality it unlocks Part one of a five-part series of blogs |
|---|
|
Excel and Python are getting ever more intertwined: you can now use the PY function to write (and call) Python code from within an Excel worksheet
|
In this blog
The PY function (allowing you to run Python code from an Excel cell) has now migrated from Excel Labs into mainstream Excel:

You won't see it in the Excel function wizard, but you should be able to type =PY( to call Python code.
If your work involves analysing data in Excel, this ability to effortlessly call Python code from within a worksheet may well change your life. This blog will get you started, showing how to call basic code and how to work with dataframes, images, modules and custom functions. You can download an Excel workbook containing all of the examples here.
Suppose you have a worksheet like this, and you want to show the total of the two numbers held in cells B1 and B2:

Sadly, you've forgotten how to create any Excel formuale, so will rely on Python code to add these two numbers together ...
Type =PY( into the cell to see this:

You're now ready to type in Python code.
Type in your Python code:

The intellisense and editor are pretty good. When you've finished you must press Ctrl + Enter to enter this code into the cell.
Notice that you don't use the return command at the end of the code, as you would if this were a Python function (doing so actually generates an error).
You'll see this while the formula calculates:

This shows that the Python code is running (for the above example it only appears for a split second).
You'll then see the result of running your code:

Who knew? 3 + 5 = 8.
Cleverly, you can click on a cell or range of cells to include a reference to it in your Python code:

Here we're about to say which cells we want to sum.
Just like in normal functions, Excel will use range names wherever they exist:

Here we've given the block of selected cells a range name, so Excel uses this instead of the cell references.
If your code contains an error when you press Ctrl + Enter the Python editor sidebar will automatically appear, but you can also invoke it by right-clicking on a cell and choosing this short-cut menu option:

Right-click on a Python code cell to show the Python editor.
You can then use this editor to make changes to your code in a fairly intuitive way:

You can scroll down to see all of your Python cells (as here), the cells containing errors or the Python cells you currently have selected, depending on your dropdown choice.
It can be useful to press Ctrl + Shift + U when editing a Python formula in an Excel cell:

When editing a formula, press Shift + Ctrl + U .
You can then edit the formula in an extended formula bar:

So you now have 3 ways to edit Python code: in-cell, using the separate Python editor or in the formula bar. Out of the 3 options I think I prefer this last one, although there's not much to choose between them.
Now that we've seen how to call simple Python code, let's have a look at some examples using dataframes.
| Parts of this blog |
|---|
|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2025. All Rights Reserved.