Inserting multiple images in Excel using OpenPyxel does not work

128 views
Skip to first unread message

pyb...@web.de

unread,
Sep 19, 2018, 5:48:03 PM9/19/18
to openpyxl-users

I'm trying to insert images with OpenPyxl to a Excel file that has already images in it (in different sheets in my case). If I do so, the existing images disappear.


Sample code:


import openpyxl
from openpyxl import load_workbook

wb = openpyxl.Workbook()

ws1 = wb.create_sheet("MySheet1")
img1 = openpyxl.drawing.image.Image('test1.png')
img1.anchor = ws1.cell(row=2, column=2).coordinate
ws1.add_image(img1)
wb.save('test_output.xlsx')

wb = load_workbook(filename='test_output.xlsx')
ws2 = wb.create_sheet("MySheet2")
img2 = openpyxl.drawing.image.Image('test2.png')
img2.anchor = ws2.cell(row=2, column=2).coordinate
ws2.add_image(img2)
wb.save('test_output.xlsx')


Is there anything I do wrong here?


Thanks alot in advance.

Charlie Clark

unread,
Sep 20, 2018, 3:44:15 AM9/20/18
to openpyx...@googlegroups.com
Am .09.2018, 23:48 Uhr, schrieb <pyb...@web.de>:

> I'm trying to insert images with OpenPyxl to a Excel file that has
> already images in it (in different sheets in my case). If I do so, the
> existing
> images disappear.

Up until very recently images in existing files were not preserved. You
need >= 2.5.5 for this.

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226

pyb...@web.de

unread,
Sep 20, 2018, 9:59:06 AM9/20/18
to openpyxl-users
Am Donnerstag, 20. September 2018 09:44:15 UTC+2 schrieb Charlie Clark:

Up until very recently images in existing files were not preserved. You  
need >= 2.5.5 for this.


Ah, I see, I'm using 2.5.3 at the moment.

Thanks !

Charlie Clark

unread,
Sep 20, 2018, 10:01:11 AM9/20/18
to openpyx...@googlegroups.com
Am .09.2018, 15:59 Uhr, schrieb <pyb...@web.de>:

> Ah, I see, I'm using 2.5.3 at the moment.

I normally try and avoid adding functionality within a release cycle for
this kind of reason but someone was kind enough to submit a PR for this
that did most of the work.
Reply all
Reply to author
Forward
0 new messages