Insert image in openpyxl

后端 未结 5 2030
不知归路
不知归路 2020-12-14 02:28

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 i

5条回答
  •  余生分开走
    2020-12-14 03:01

    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')
    

提交回复
热议问题