Python Excel 写入器 (xlswriter) 从 URL 插入图片

1 投票
2 回答
5232 浏览
提问于 2025-04-18 08:41

我想知道怎么用 xlswriter 从网址(http)插入一张图片。这是文档中的内容:

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

或者

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

不过这些只适用于文件路径。我想从网络服务器的 URL 添加图片。你能帮忙吗?

2 个回答

4
# 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})

这是一个链接,指向一个网站,里面有关于如何使用xlsxwriter这个工具的示例,特别是关于如何处理图片和字节流的内容。你可以点击这个链接去了解更多信息。

1

在编程的世界里,有时候我们会遇到一些问题,特别是在使用某些工具或库的时候。这些问题可能会让我们感到困惑,但其实大多数时候,解决方案就在我们身边。我们只需要仔细查看错误信息,或者在网上寻找相关的讨论和解决办法。

例如,有人可能会在使用某个库的时候遇到错误,这时候可以尝试查看这个库的文档,或者在社区论坛上询问其他开发者的意见。很多时候,别人也遇到过类似的问题,他们的经验可能会帮助我们更快地找到解决方案。

总之,遇到问题时不要慌张,保持冷静,利用好网络资源,通常都能找到答案。

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

撰写回答