Ein Webbild von einer URL in ein Excel Arbeitsblatt laden
Laden eines Bildes von einer URL in ein Excel-Arbeitsblatt
Aspose.Cells für Python via .NET API bietet eine einfache und unkomplizierte Möglichkeit, Bilder aus URLs in Excel-Tabellenblätter zu laden. Dieser Artikel erklärt das Herunterladen der Bilddaten in einen Stream und das Einfügen in das Tabellenblatt mit der Aspose.Cells für Python via .NET API. Mit dieser Methode werden die Bilder Teil der Excel-Datei und werden nicht bei jedem Öffnen des Tabellenblatts erneut heruntergeladen.
Beispielcode
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) |