I want to insert a Image(object) in MS-Excel Report which i am generating using openpyxl utility. Is there a way to do it using some python utility?
1 Answer
Openpyxl allows you to write images into your Excel files! Here it is in the official documentation.
import openpyxl
wb = openpyxl.Workbook()
ws = wb.worksheets[0]
picture = openpyxl.drawing.Image('/path/to/picture')
picture.anchor(ws.cell('cell to put the image'))
ws.add_image(picture)
wb.save('whatever you want to save the workbook as')
This code of course refers to creating a new workbook and adding the image into it. To add the image to your preexisting workbook you would obviously just load that workbook using load_workbook.
3 Comments
Deepak Kumar
I am able to add the object using the code suggested by you. But i was looking for a code which can insert it as an attachment not as file as whole.
Charlie Clark
What do you mean by "attachment"? The OOXML specification expects all media files to be included in the package and this is what openpyxl does.
Deepak Kumar
It's very clear : I want to insert an object and display it as an icon. I don't want to insert picture spanning several cells.