Starting With "Hello World"
Here are resources for more information about working with virtual environments, pip and bpython:
00:00
To get started with openpyxl
, create a virtual environment in a new folder. I’m going to use venv
, but you can use any environment manager.
00:11 Once you have it, go ahead and activate it
00:17
and then pip install openpyxl
. I’m also going to grab bpython
to clean up some terminal sessions later.
00:29
While that’s installing, go ahead and make a new file. I’m going to call mine hello_openpyxl.py
Inside this new file, I’m going to say from openpyxl import Workbook()
00:47
and then define workbook
as a new Workbook
. Then, sheet
will equal workbook.active
. With sheet
, say that "A1"
is equal to "hello"
, and then "B1"
is going to be equal to "world!"
.
01:10
If this doesn’t make sense, don’t worry. We’re going to talk about it in a little bit. Finally, say workbook.save()
and pass it in the filename
, like "hello_world.xlsx"
.
01:22
Save this, and then go ahead and run it. Okay. If everything went well, you should have a new file called "hello_world.xlsx"
. If you go to open that up, you should see hello and world! in A1 and B1. All right!
01:42
You’ve just made your first spreadsheet using openpyxl
. If we go back here, we can see that the spreadsheet, or Workbook
, is a class inside openpyxl
, and then within each Workbook
is a number of sheets. Here, by taking the active sheet in the workbook, you’ve just selected whichever sheet is open when you open the workbook normally. Each sheet is then broken up into a number of cells, which here are "A1"
and "B1"
, and then these cells are organized into rows and columns, which are represented by letters for columns and numbers for the rows.
02:17
Before we finish up in this video, go ahead and download the sample Excel worksheet from the resources (Supporting Material) tab. It contains a number of reviews of watches from Amazon, and you’ll use that to practice the different features of openpyxl
.
Become a Member to join the conversation.
Coxy on Nov. 7, 2023
Hi Joe, I have followed the instructions and having an issue with bpython?
I am using VScode IDE, on a windows 11 machine. Would you be able to point me in the right direction please.
Thank you