gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
can I get the code to save image into excel using python loop
- User
can I get the code to save image into excel using python loop
- Assistant
Certainly! Here's an example Python code that uses a loop to save multiple images into an Excel file using the
openpyxllibrary:pythonimport openpyxl from openpyxl.drawing.image import Image # Create a new workbook workbook = openpyxl.Workbook() sheet = workbook.active # List of image file paths image_files = ["image1.png", "image2.jpg", "image3.bmp"] # Loop through the image files and save them in Excel for i, image_file in enumerate(image_files): img = Image(image_file) cell = "A{}".format(i + 1) # Place the image in the first column sheet[cell].value = "Image {}".format(i + 1) sheet.add_image(img, cell) # Save the workbook workbook.save("images.xlsx")In this example, the
image_fileslist contains the paths to the images you want to save in Excel. The loop iterates over the list, creates an object for each image file, and inserts it into an Excel cell using the method. Finally, the workbook is saved with the images using the method.