32

Is it possible to insert an image (jpeg, png, etc) using openpyxl?

Basically I want to place a generated image with a chart below it.

I don't see anything in the documentation, which seems to be a little lacking compared to the maturity of the code.

4 Answers 4

48

The following inserts an image in cell A1. Adjust the image location to your needs or handle the creation of the PIL image yourself and hand that to Image()

import openpyxl

wb = openpyxl.Workbook()
ws = wb.worksheets[0]
img = openpyxl.drawing.image.Image('test.jpg')
img.anchor = 'A1'
ws.add_image(img)
wb.save('out.xlsx')

In older versions of openpyxl the following works:

import openpyxl

wb = openpyxl.Workbook()
ws = wb.worksheets[0]
img = openpyxl.drawing.Image('test.jpg')
img.anchor(ws.cell('A1'))
ws.add_image(img)
wb.save('out.xlsx')
9
  • are gif files supported?
    – user299709
    Dec 26, 2014 at 7:33
  • 1
    I am using Xlsxwriter and it doesn't support embedding a gif image so im wondering if openpyxl does.
    – user299709
    Dec 26, 2014 at 7:41
  • 1
    @user299709 It is clear to me that you are wondering about that. The answer lies in what file formats PIL supports and I find it difficult to believe you could not find that for yourself using google.
    – Anthon
    Dec 26, 2014 at 7:45
  • 9
    As of 2017, it looks like: openpyxl.drawing.Image has moved to: openpyxl.drawing.image.Image Nov 20, 2017 at 17:41
  • 2
    As of 2019 img.anchor(ws.cell('A1')) now is img.anchor = 'A1'
    – jonyfries
    Apr 12, 2019 at 20:17
9

Providing a full update on how to do this. This solution uses openpyxl version 2.4.5.

I downloaded an image to my local directory, opened an existing workbook and saved with the image inserted.

import openpyxl
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.drawing.image import Image
from openpyxl.utils import coordinate_from_string

openpyxl_version = openpyxl.__version__
print(openpyxl_version)  #to see what version I'm running

# downloaded a .png to local directory manually from
# "https://www.python.org/static/opengraph-icon-200x200.png"

#change to the location and name of your image
png_loc = r'c:\users\me\opengraph-icon-200x200.png'

# test.xlsx already exists in my current directory 

wb = load_workbook('test.xlsx')
ws = wb.active
my_png = openpyxl.drawing.image.Image(png_loc)
ws.add_image(my_png, 'B3')
wb.save('test.xlsx')

Results:

enter image description here

3
  • ws.add_image(my_png, 'B3') triggers a warning DeprecationWarning: Call to deprecated function anchor (Anchors can be passed in when an image is added to a worksheet). any idea how to fix it?
    – Imad
    Sep 10, 2018 at 15:34
  • Using openpyxl version 2.5.5 and 2.5.6 (the latest stable release), I was unable to reproduce this warning, running the code as posted in my example. Sep 11, 2018 at 13:50
  • My openpyxl version is less recent. I'll make sure to update it
    – Imad
    Sep 12, 2018 at 16:43
1

This code worked for me:

import openpyxl

wb = openpyxl.Workbook()
ws = wb.worksheets[0]
ws.merge_cells('A1:A3')
img = openpyxl.drawing.image.Image('image.jpg')
row_number = 1
col_idx = 1
cell = ws.cell(row=row_number, column=col_idx)
ws.add_image(img)
wb.save('output.xlsx')
1
1

Just to add, I have been using openpyxl==2.5.6 (with Python3.65), and I had to use img.anchor('A1') instead of img.anchor(ws.cell('A1')).

import openpyxl

wb = openpyxl.Workbook()
ws = wb.worksheets[0]
img = openpyxl.drawing.Image('test.jpg')
img.anchor('A1')
ws.add_image(img)
wb.save('out.xlsx')
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.