kadir_cakir
kadir_cakir

Reputation: 1073

Python Excel Writer (xlswriter) Insert Image from URL

How can I insert an image from URL (http) with xlswriter? This is from documentation:

worksheet.insert_image('B2', 'python.png')

or

worksheet1.insert_image('B10', '../images/python.png')

But this is only for file path. I want to add image from URL from a Web Server. Can you help?

Upvotes: 1

Views: 5447

Answers (2)

Hariprasad
Hariprasad

Reputation: 1653

# Read an image from a remote url.
url = 'https://raw.githubusercontent.com/jmcnamara/XlsxWriter/' + \
      'master/examples/logo.png'

image_data = BytesIO(urllib2.urlopen(url).read())

# Write the byte stream image to a cell. Note, the filename must be
# specified. In this case it will be read from url string.
worksheet.insert_image('B2', url, {'image_data': image_data})

http://xlsxwriter.readthedocs.io/example_images_bytesio.html

Upvotes: 4

user3710586
user3710586

Reputation: 26

url = "http://abcdef.com/picture.jpg"
data = urllib.request.urlopen(url).read()
file = open("image.jpg", "wb")
file.write(data)
file.close()
worksheet.insert_image('B2', 'image.jpg')

Upvotes: 1

Related Questions