简体   繁体   English

Python Excel Writer(xlswriter)从URL插入图像

[英]Python Excel Writer (xlswriter) Insert Image from URL

How can I insert an image from URL (http) with xlswriter? 如何使用xlswriter 从URL (http)插入图像? 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. 我想从Web服务器添加URL中的图像。 Can you help? 你能帮我吗?

# 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 http://xlsxwriter.readthedocs.io/example_images_bytesio.html

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM