简体   繁体   中英

How do I insert multiple non-overlapping images using openpyxl?

I want to insert multiple images into a worksheet, but I cannot figure out how to automatically insert them such that they do not overlap (nor can I find good documentation on this).

For example:

sheet["A1"] = "Here is an image"
img = Image('image.png')
img.anchor(sheet.cell("A2"))
sheet.add_image(img)
sheet["A3"] = "Here is another image"
img2 = Image('image.png')
img2.anchor(sheet.cell("A4"))
sheet.add_image(img2)

This ends up with the images stacked overlapping each other, rather than one after the other. How do I get them to appear sequentially?

There is no way to do this automatically. Images are anchored either on one cell, between two cells, or using absolute coordinates. Using cell anchors you'll have to guess how many cells the images are likely to cover (it will vary from OS to OS depending on the system DPI setting). So the most reliable thing would be to use absolute anchors.

Although this is possible it isn't really documented so you'll have to look through the code, particular the tests, and familiarise yourself with EMUs used by Excel for positioning content.

You might want to continue discussion on the mailing list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM