【发布时间】:2017-08-10 01:31:48
【问题描述】:
我想将 URL 中的图像插入 xlsx 文件。 我怎么能用 openpyxl 做到这一点? 我检查了文档,但没有显示如何从 URL 插入图像。
【问题讨论】:
标签: python image python-3.x url openpyxl
我想将 URL 中的图像插入 xlsx 文件。 我怎么能用 openpyxl 做到这一点? 我检查了文档,但没有显示如何从 URL 插入图像。
【问题讨论】:
标签: python image python-3.x url openpyxl
Openpyxl 中没有内置函数通过 URL 插入图像。您需要为 python 使用 Http 客户端模块。(例如: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')
【讨论】:
pil 进行操作。看到这个答案:stackoverflow.com/a/37631799/7683374