I would like to insert an image from URL into the xlsx file. How could I do that with openpyxl? I checked the documentation but not shows how to insert an image from URL.
Asked
Active
Viewed 3,864 times
1 Answers
9
There is no built-in function in Openpyxl
to insert images through URLs. You'll need to use an Http client module for python.(example:urllib
)
import openpyxl
from openpyxl.writer.excel import save_virtual_workbook
from openpyxl.drawing.image import Image
import PIL
import io
import urllib3
wb = openpyxl.Workbook()
ws = wb.active
r = 1
ws['A1'] = 'test'
http = urllib3.PoolManager()
r = http.request('GET', 'http://myridia.com/assets/images/logo.png')
image_file = io.BytesIO(r.data)
img = Image(image_file)
ws.add_image(img, 'A2')

saud
- 678
- 1
- 12
- 24
-
Is there anyway, I can compress the image size to fix dimension lets say 250x150 from the network image? – newbie dev May 17 '21 at 04:03
-
@newbiedev looking at the [API doc](https://openpyxl.readthedocs.io/en/stable/api/openpyxl.drawing.image.html), it doesn't seem to have options to resize the image. You could instead try doing it with `pil`. See this answer: https://stackoverflow.com/a/37631799/7683374 – saud May 17 '21 at 18:40