使用URL复制图像并将其粘贴到Excel中,可以通过以下步骤实现(使用xlwings库):
import requests
from PIL import Image
import io
import xlwings as xw
image_url = "https://example.com/image.jpg"
response = requests.get(image_url)
image = Image.open(io.BytesIO(response.content))
app = xw.App(visible=False)
workbook = app.books.add()
worksheet = workbook.sheets[0]
cell_range = worksheet.range('A1')
worksheet.pictures.add(image, left=cell_range.left, top=cell_range.top, width=image.width, height=image.height)
workbook.save("output.xlsx")
workbook.close()
app.quit()
完整的代码示例:
import requests
from PIL import Image
import io
import xlwings as xw
image_url = "https://example.com/image.jpg"
response = requests.get(image_url)
image = Image.open(io.BytesIO(response.content))
app = xw.App(visible=False)
workbook = app.books.add()
worksheet = workbook.sheets[0]
cell_range = worksheet.range('A1')
worksheet.pictures.add(image, left=cell_range.left, top=cell_range.top, width=image.width, height=image.height)
workbook.save("output.xlsx")
workbook.close()
app.quit()
这样,你就可以使用URL复制图像并将其粘贴到Excel中,而无需先下载图像。
领取专属 10元无门槛券
手把手带您无忧上云