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 four 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
For this part of the blog I want to show why we haven't needed to reference any of the modules we've used (pandas, PIL, etc.), and how the Python virtual enviornment within Excel works.
To see what Excel includes by default when you insert Python, choose this option on the Formulas tab of the ribbon:

This option will show you the code which runs at the start of any Python program that you insert into Excel.
What you should then see is this in the Python editor sidebar:

This code runs in the background so that you have automatic access to Pandas, NumPy, MatPlotLib and Excel, among other modules, using the usual default aliases.
But surely this is not complete, as it doesn't include PIllow, and yet in the previous part of this blog I was able to work with the PIL library without importing it? I suspect that Excel does import the PIL module, but it's just not shown in this initialisation help feature.
Suppose you want to use the Python Faker module to generate a list of 5 random (made-up) females names:

Whenever you recalculate the worksheet this list should change.
You could do this with the Faker module, but you would have to install it otherwise you'd get this error:

If you try to reference a module which isn't imported, you'll see this error.
Here's what your Python code could look like for this example:
# import the necessary module
from faker import Faker
# create a faker session
fake = Faker()
# generate a list containing 5 names
fake_names = []
for number in range(5):
fake_names.append(fake.name_female())
# now show these names
fake_names
# note - could have done with single line comprehension
# [fake.name_female() for _ in range(5)]
Python within Excel uses a virtual environment in the cloud (at least, that's the way I choose to understand it). You can see which modules are available in this environment here:

This page warns you that you won't be able to do some things in Python (the link shown goes to this page).
So although (for example) you can import the requests module, you won't be able to use it to access a website:

This Python code would return 403, the HTTP status code meaning access to the requested resource is forbidden.
I don't think it's possible to call your own modules, as you'd somehow need to upload these first into Excel's virtual environment in the cloud, but you can call functions you've created, as shown in the final part of this blog.
| 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.