Skip to content
Home » openpyxl Part 17 – Adding Images

openpyxl Part 17 – Adding Images

Spread the love

In the previous couple parts of the openpyxl series we were talking about styling spreadsheets, in particular about conditional styling. In the previous part we were adding data bars and before that we were adding icons. These are all graphical elements that represent data in one way or another. But sometimes you may want to add a graphical element that doesn’t represent data, or, to be more precise, is independent of the data. In this part we’ll see how to add simple images in openpyxl to our workbook.

We’re still going to be working with our wb2.xlsx file. It contains the following data:

companycitycategoryincomenumber_of_employees
Roses and PansiesTorontogardening456845.0026
Reader’s ParadiseSydneypublisher911500.0044
Dogs and CatsMadridpets458784.0036
Enjoy Your MealNew Orleansrestaurant447884.0018
Bloom StoreLondongardening1254654.0084
Tastes of the SouthMiamirestaurant587454.0042
Pets for EveryoneAthenspets987887.0021
Eat ‘Em UpHelsinkirestaurant1254477.0053
Mr. BunnyViennapets254478.0012
Bookworms RuleRomepublisher124474.0040
SUM376
MIN12
MAX84
AVERAGE37.6
BIG COMPANIES4

As usual, let’s start by loading the workbook:

 >>> from openpyxl import load_workbook
 >>> workbook = load_workbook(filename="wb2.xlsx")
 >>> sheet = workbook.active 

I’m going to add the following image to my workbook:

openpyxl

The image file is saved in the same folder where the workbook is. Its name is ‘Exploding Planet.png’. You can use whatever image you want.

Adding an image is easy. There are just a few steps to follow. First of all, we’ll need the Image class, so let’s import it:

 >>> from openpyxl.drawing.image import Image 

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Next we have to create an image instance and pass the name of the image file to the constructor:

 >>> image = Image('Exploding Planet.png') 

The size of my image is 540 by 540 pixels. This is a bit more than I need. So, let’s set the width and height both to 90 px:

 >>> image.width, image.height = 90, 90 

Now we have to actually add the image. We have to specify the image that we want to add and the location of its top left corner:

  >>> sheet.add_image(image, 'C15') 

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Finally, let’s save the workbook with the image as a new file:

 >>> workbook.save(filename="wb2i.xlsx") 

After that we can open the wb2i.xlsx file and see the image in it:

workbook

Now that we know how to add styles, small graphical items like icons and data bars and images, there’s still one important graphical element that we often want to add to our spreadsheets, a chart. This element by all means depicts the data. There are several types of charts. We’ll start talking about charts in the next part of the series.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love
Tags:

Leave a Reply