How to move and duplicate multiple images in Excel worksheets using Openpyxl?

463 views
Skip to first unread message

Dave Hartsough

unread,
Oct 27, 2020, 10:34:19 AM10/27/20
to openpyxl-users

I have Excel worksheets within which I would like to move / duplicate multiple images.

Using Openpyxl, I can insert images into a worksheet from a file (.png or .jpg file), but multiple copies of the same image causes the file to be corrupted upon save due to Zip issues - and the images need to come from a file not elsewhere in the workbook.  I think I can resolve the Zip issues by renaming the images, but the source of the images is still an issue.

Is there a way in Openpyxl to move / duplicate existing images.  Thanks in advance for any help.


-dave

Charlie Clark

unread,
Oct 27, 2020, 11:02:01 AM10/27/20
to openpyxl-users
There is a bug for this:

https://foss.heptapod.net/openpyxl/openpyxl/-/issues/1532

I must admit I haven't looked at this in detail but we probably need
some code that can check image file names when serialising to the
zipfile and adjust these as necessary.

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

Dave Hartsough

unread,
Oct 27, 2020, 11:09:12 AM10/27/20
to openpyx...@googlegroups.com
Charlie,

  Thanks for the prompt reply on the Zip / Image name issue.  Do you have any insights on the part related to the source of the images?  Specifically, do you know if it is possible in Openpyxl to insert / duplicate images that originate on a worksheet rather than from an external .png or .jpg?

-dave

--
You received this message because you are subscribed to the Google Groups "openpyxl-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openpyxl-user...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/openpyxl-users/6AF4CEBA-DD2F-4C57-8760-2948C78C802E%40clark-consulting.eu.

Charlie Clark

unread,
Oct 27, 2020, 11:16:02 AM10/27/20
to openpyx...@googlegroups.com
On 27 Oct 2020, at 16:08, Dave Hartsough wrote:

> Charlie,
>
> Thanks for the prompt reply on the Zip / Image name issue. Do you
> have
> any insights on the part related to the source of the images?
> Specifically, do you know if it is possible in Openpyxl to insert /
> duplicate images that originate on a worksheet rather than from an
> external
> .png or .jpg?

It should be possible, yes. Images are kept in the _images list of a
worksheet so you should be able to find one (using the anchor element),
make a copy of it and add this to another worksheet.

The code for this could be a bit fiddly because I've never really looked
at managing images. They're a bit fiddly as they exist in a layer above
a worksheet and can be "anchored" differently: to one cell, between two
cells, and absolutely based on their position from the top left corner
of the worksheet (or it might be screen). This makes it hard to name
them. So much more intuitively in a GUI.

But basically, if you're prepared to dive into the source code and right
the relevant to make copies then it should be possible without too much
work.
Reply all
Reply to author
Forward
0 new messages