【问题标题】:Insert an image from url in openpyxl在openpyxl中从url插入图像
【发布时间】:2017-08-10 01:31:48
【问题描述】:

我想将 URL 中的图像插入 xlsx 文件。 我怎么能用 openpyxl 做到这一点? 我检查了文档,但没有显示如何从 URL 插入图像。

【问题讨论】:

标签: python image python-3.x url openpyxl


【解决方案1】:

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

Source: Insert image from URL in openpyxl.

【讨论】:

  • 无论如何,我可以压缩图像大小以固定尺寸,比如网络图像的 250x150?
  • @newbiedev 看着API doc,它似乎没有调整图像大小的选项。您可以改为尝试使用pil 进行操作。看到这个答案:stackoverflow.com/a/37631799/7683374
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-24
  • 2020-10-18
  • 1970-01-01
  • 2011-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多