Cargar una Imagen Web desde una URL en una Hoja de Excel

Cargar una imagen desde una URL en una hoja de Excel

La API Aspose.Cells para Python via .NET proporciona una forma sencilla y fácil de cargar imágenes desde URLs en las hojas de cálculo de Excel. Este artículo explica cómo descargar los datos de la imagen en un flujo y luego insertarlos en la hoja de cálculo usando la API Aspose.Cells para Python via .NET. Con este método, las imágenes se vuelven parte del archivo de Excel y no se descargan cada vez que se abre la hoja de cálculo.

Código de Muestra

import requests
from PIL import Image
import io
from aspose.cells import Workbook, CellsHelper
# For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
# Define memory stream object
obj_image = io.BytesIO()
# Define a string which will hold the web image url
s_url = "https://docs.aspose.com/cells/images/Aspose-image-for-open-graph.jpg"
try:
# Instantiate the web client object
response = requests.get(s_url)
# Now, extract data into memory stream downloading the image data into the array of bytes
obj_image.write(response.content)
# Create a new workbook
wb = Workbook()
# Get the first worksheet in the book
sheet = wb.worksheets[0]
# Get the first worksheet pictures collection
picts = sheet.pictures
# Insert the picture from the stream to B2 cell
picts.add(1, 1, obj_image)
# Save the excel file
wb.save("webimagebook.out.xlsx")
except Exception as ex:
# Write the error message on the console
print(ex)