Locked lesson.
About this lesson
openpyxl is the Python library that we'll use to connect our Python code to Excel.
Exercise files
Download this lesson’s related exercise files.
12 - Pip Install openpyxl and Import.docx57 KB 12 - Pip Install openpyxl and Import SOLUTION.docx
55.5 KB
Quick reference
Pip Install openpyxl and import
openpyxl is the Python library that we'll use to connect our Python code to Excel.
When to use
Use this anytime you need to access an Excel spreadsheet in your Python code.
Instructions
The openpyxl documentation can be found here: https://pypi.org/project/openpyxl
To install, go to your terminal and make sure your virtual environment is turned on, then issue this command:
pip install openpyxl
Hints & tips
- pypi.org/project/openpyxl
- pip install openpyxl
- 00:04 Okay, so we're pretty much done learning the basics of Python.
- 00:07 Now obviously, there's a lot more Python you can learn.
- 00:10 But for the purposes of this course, I think the things we've
- 00:13 learned in the last few videos will really give us a solid grounding and
- 00:16 a foundation that we can build on.
- 00:18 So now we want to switch gears and start talking about OpenPyXl.
- 00:21 So the premise of this course is we want to be able to do Python coding with Excel
- 00:26 spreadsheets.
- 00:27 We want to be able to connect to an Excel spreadsheet and either get information,
- 00:31 get data from the Excel spreadsheet and bring it into our Python program and
- 00:35 do stuff with it.
- 00:36 Or go the other direction, do something with our Python programming, and
- 00:40 then save our data to an Excel spreadsheet on its own.
- 00:44 So to do that, we have to be able to connect to Excel.
- 00:47 And the Python library that we use to do that is called OpenPyXl.
- 00:51 Now, this does not come with Python, we're going to have to download and
- 00:54 install that, and that's what we'll do in this video.
- 00:56 But before we do that, I just want to talk a little bit about what this is and
- 00:59 where you can get more information.
- 01:01 Now obviously, we're going to spend the next 10 or 15 or 20 videos going through
- 01:05 this in great detail, but I always like to point students towards the documentation.
- 01:10 So it really doesn't matter what you're learning,
- 01:13 you always want to kind of at least be aware of the documentation.
- 01:16 Where is it?
- 01:16 Where does it exist?
- 01:17 If you need to look something up, where can you do that?
- 01:20 So let's head over to our web browser really quickly and
- 01:26 go to pypi.org/project/openpyxl.
- 01:32 And here is sort of the website for it.
- 01:36 And you can see, openpyxl is a Python library to read/write
- 01:41 Excel 2010, xlsx/xlsm/xltx/xltm files.
- 01:46 Now don't be scared when you see this 2010,
- 01:48 obviously that's a very old version of Excel.
- 01:51 OpenPyXl works with basically all versions of Excel,
- 01:53 including the most current version, which we're going to be using in this course.
- 01:57 So like I said, this will allow us to read and
- 02:00 write natively from Python to Excel, right?
- 02:03 And if you want to look at the documentation for this thing,
- 02:05 you can come down here, and here's a link to the actual documentation.
- 02:09 And you'll see there's a lot of stuff here that you can read.
- 02:12 And this is a pretty good resource to be familiar with, if you want to come over
- 02:16 here and just sort of browse through here and see what's available.
- 02:20 This is also a great place to refer back in the future after you've watched these
- 02:24 videos.
- 02:25 If you don't really remember something, you want to just look it up very quickly.
- 02:29 You can come down here and just click on a thing and probably find it very quickly.
- 02:33 So, okay, those are the documents.
- 02:35 That's the documentation, pretty simple, pretty straightforward.
- 02:38 So now let's install OpenPyXl.
- 02:40 So let's head over to our terminal,
- 02:42 make sure you're in your c/python-excel directory.
- 02:45 And most importantly, make sure your virtual environment is turned on.
- 02:49 Because we want to install OpenPyXl into our virtual environment.
- 02:53 So before we install it,
- 02:54 let's see what's already installed inside of our virtual environment.
- 02:58 And we can do that using the PIP freeze command.
- 03:02 And you can see nothing is returned because we haven't installed anything into
- 03:05 our virtual environment yet.
- 03:07 We created it, but we haven't installed anything in there.
- 03:10 So now we're going to install our first thing, the OpenPyXl library.
- 03:13 So to do that, we go pip install openpyxl, and these are all lowercase.
- 03:20 And PIP, if you remember when we installed Python, I mentioned stands for
- 03:24 the Python Installation Program.
- 03:26 And what it does is it goes online, grabs whatever you want,
- 03:29 brings it back and installs it wherever you are.
- 03:32 In our case, we're in this virtual environment, so
- 03:34 it will install things inside of our virtual environment.
- 03:36 And what do we want to do?
- 03:37 We want to install.
- 03:38 What do we want to install?
- 03:40 Openpyxl, and it's really just this easy.
- 03:42 So let's hit Enter.
- 03:44 And you can see it's collecting it.
- 03:45 It's doing some things.
- 03:47 It's installing different packages that it's going to need.
- 03:50 And then boom, it's done.
- 03:52 Now I'm getting a message saying my PIP installation is a little outdated and
- 03:56 I should upgrade.
- 03:57 You can absolutely just ignore that if you get that same message.
- 04:01 So okay, that's it.
- 04:02 Openpyxl has now been installed.
- 04:04 If we clear the screen, and now if we run that same pip freeze command, we
- 04:09 can see several things had been installed, we've got openpyxl version 3.0.4.
- 04:15 Again, if this is a different version by the time you watch this video,
- 04:18 no big deal, whatever the latest version is, is fine.
- 04:20 And then we've also got this gdcal and
- 04:22 this et-xmlfile thing,and we don't really care what these things are.
- 04:27 They're just things that OpenPyXl needs in order to run, so
- 04:31 it has installed them for us.
- 04:33 So that's really all there is to it.
- 04:35 Now, in order to use OpenPyXl in our program, or
- 04:39 really in any program, all you have to do is come to the top and
- 04:43 just import openpyxl, and that's all there is to it.
- 04:47 Now we're going to be changing this import statement in the next video,
- 04:51 as we get more specific and start importing specific things from OpenPyXl.
- 04:55 But strictly speaking, that's how you import really just about anything using
- 04:59 Python is just declare it up at the top of your file.
- 05:02 So that's all for this short intro video.
- 05:04 I just wanted to get this thing installed in our system and show you
- 05:07 where the documentation is, if just in case you want to read more about this.
- 05:11 In the next video, we'll jump right in and start working with this thing.
Lesson notes are only available for subscribers.