Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert image in openpyxl

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.

like image 830
tomc Avatar asked Jun 04 '12 21:06

tomc


People also ask

How do you insert a Picture in a cell?

All you have to do is these 3 quick steps: In your Excel spreadsheet, click where you want to put a picture. Switch to the Insert tab > Illustrations group, and click Pictures. In the Insert Picture dialog that opens, browse to the picture of interest, select it, and click Insert.

How do I insert an image into XlsxWriter?

It is possible to insert an image object at a certain cell location of the worksheet, with the help of insert_image() method. Basically, you have to specify the location of cell using any type of notation and the image to be inserted. The insert_image() method takes following optional parameters in a dictionary.


1 Answers

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') 
like image 77
Anthon Avatar answered Sep 18 '22 19:09

Anthon