Locked lesson.
About this lesson
We'll cover how to add images to your spreadsheet.
Exercise files
Download this lesson’s related exercise files.
40 - Adding Images.docx57.4 KB 40 - Adding Images SOLUTION.docx
56.2 KB
Quick reference
Adding Images
In this video I'll show you how to add Images to your spreadsheet.
When to use
Use this whenever you want to add an image to your spreadsheet.
Instructions
# Install Pillow to the terminal
pip install Pillow
# Install Libraries
from openpyxl.drawing.image import Image
# create Image instance
img = Image("aspen.png")
# Set image height and width
img.height = 50
img.width = 50
# add image to worksheet
ws.add_image(img, 'D2')
Hints & tips
- pip install Pillow
- from openpyxl.drawing.image import Image
- img = Image("aspen.png")
- ws.add_image(img, 'D2')
- img.height = 50
- img.width = 50
- 00:04 Okay in this video I'm going to show you how to add images to a spreadsheet with
- 00:08 OpenPyxl.
- 00:08 So I'm in our working directory here and I've added this picture aspen.png.
- 00:12 And if you take a look at this,
- 00:13 it's just a picture of me and my dog aspen being goofy.
- 00:16 So, notice we've added this file to this directory and that makes this very easy.
- 00:21 So, in order,
- 00:22 to use images with OpenPyxl we need to install something called Pillow.
- 00:26 And to do that we head over to our terminal, and we can just go pip install
- 00:31 Pillow, and notice it's capital P, this will grab Pillow and install it.
- 00:36 Pillow is short for PIL which is short for Python Image Library.
- 00:40 Now, some people have trouble with Pillow and it just won't work.
- 00:45 If this is you, if this happens to you, and you try and install this and
- 00:48 this doesn't work, you're going to have to try and
- 00:51 Google the error if you get an error when you run your file.
- 00:53 Otherwise, you may just be out of luck, but for
- 00:56 like 80 to 90% of Windows users, this should work just fine.
- 01:00 So, okay, we've installed Pillow.
- 01:02 Now, we need to come over here and add something to our OpenPyxl worksheet and
- 01:06 I'm just using the same file we used in the last video for our table.
- 01:10 So come up to the top and let's go from
- 01:15 openpyxl.drawing.image, we need to import image.
- 01:21 Now, we can come down here, and let's add our image.
- 01:24 So I'm going to create a variable, I'm just going to call it img short for image.
- 01:28 And we just want to create an image instance and
- 01:31 then just set the name of our picture, so ours is called aspen.png.
- 01:36 Now, since I saved this file in the same directory, I can just use a relative path.
- 01:40 Otherwise, you would have to do open, whatever, directory,
- 01:45 wherever, the file is actually sitting on your computer,
- 01:49 you don't have to put it explicitly.
- 01:52 But as I said, since this file is in this same directory as this file,
- 01:56 we can just use the relative path like that.
- 01:58 So, all right, we've defined our image, now,
- 02:01 all we have to do is add it to our our worksheet.
- 02:05 So we can go ws.add_image and then what do we want to set?
- 02:09 We want to set the image, and where do we want to put it, let's put it in D2.
- 02:15 So, now we could just save this and head over here and run this again.
- 02:19 So python table.py, and then head over here and let's run and
- 02:24 let's open our spreadsheet and boom, we get, image put right in there.
- 02:31 So this is the actual size of the image.
- 02:33 Obviously, you can resize it like this, but
- 02:35 if you wanted to resize it programmatically, it's fairly simple.
- 02:39 We can do that, we just set the image height and width right here.
- 02:44 So let's go img.height,
- 02:46 and set that equal to let's make it half the size of its normal height.
- 02:51 So we can go img.height / 2, and
- 02:54 we can do the same thing for width, img.width,
- 03:00 set that equal to img.width / 2.
- 03:04 And this forward slash is divided if you wanted it twice as big you could multiply,
- 03:09 here is the multiplication sign, right?
- 03:11 So any sort of math operators you can use.
- 03:14 So let's go ahead and save this and run it again.
- 03:17 And if we open this, we can see now it's half as big as it was.
- 03:20 So we did that programmatically using percentage or using percentages,
- 03:25 we made it half as big.
- 03:27 You can be very explicit, you could say, make this 50 pixels by 50 pixels.
- 03:32 This is going to be very small, but if we save this and run it,
- 03:37 and then open this guy, he sees a very small 50 pixels.
- 03:42 And shoved right up in there and it's not really in proportion anymore.
- 03:45 But that's how you can change it to any sort of size that you want very
- 03:49 explicitly, you don't have to use math.
- 03:51 So that's how to add images to your Excel Spreadsheet, very simple and
- 03:56 pretty straightforward.
- 03:57 So that's all for this video.
Lesson notes are only available for subscribers.