Excel + Python (p.1)

We ca finally use Python into Excel directly, so let’s start using it. This is avaiable into the Microsoft 365 insider beta channel for the moment.

The code

We can put the code into a cell by doing =py (and then press TAB) or pressing the python icon into the formulas tab.

We ca finally use Python into Excel directly, so let’s start using it. This is avaiable into the Microsoft 365 insider beta channel for the moment. Write =py into a cell and then press TAB and you can start writing your code or press the python icon in the formulas tab of the Excel menu. You can run the script pressin ctrl+enter. To see the values you can use print, but you will see the result into the diagnostic panel only (activable pressing diagnostic near the python icon in the formulas tab). To see the values in the cells, do not use print, but just write the variable you want to get into the cells (if you separate them with a comma you will see them one under the other in different cells) and then go in the little arrow near the py text of the formulas on green background and choose excel values instead of python object, pressi ctrl+enter and you will get them.

You can see the code in the formulas tab

import datetime

today = datetime.datetime.today()
today.day, today.month, today.year

Just three lines of code. We get the time from the datetime module and the today function.

How to get the result in cells

Now, if we use print we will only get the result into the diagnostic panel

so, if we press ctrl + enter we wee this on the diagnostic panel on the right

1 the cell where the code is

2 the year

3 the month

Let’s see if the formatting works in Excel too

Result in the diagnostic

It worked, so we can check our variables better.

Now, how to get some values into the Excel cells?

Simply doing this:

Click on the arrow (1) and then on value of Excel. You will get this:

I commented the print statement, otherwise we wouldn’t have seen the values.

So now the values are accessible into excel.

Another way to get the value in a cell

Another way is to go into another cell and put = and then the cell where the value was (C3). Another is to create another python cell and write the variable into as you can see in the picture here.

We can of course do operation with these values from python cells into regular Excel cells, like this shown in the picture below

Ok, this is our first (not the very first, but the first of a new series maybe) sneak peek into Excel with Python. See ya next time for part 2.

https://youtube.com/shorts/MGy1XK0CfRg


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.