Link to home
Start Free TrialLog in
Avatar of Norie
Norie

asked on

Name images inserted on Excel worksheet using Python

I want to name images I'm inserting into an Excel workbook using Python.

I've tried using both openpyxl and xlsxwriter to insert the images but both give default names to the inserted image, e.g. Image 1, Picture 1.

Is there a way to name the inserted images, preferably taking the name from the filename?

Here's my code so far.
import xlsxwriter
import openpyxl

wb = openpyxl.Workbook()
ws = wb.worksheets[0]

workbook = xlsxwriter.Workbook('Maps.xlsx')
worksheet = workbook.add_worksheet('Maps')

images = ['overview.png','Site 1.png','Site 2.png','Site 3.png','Site 4.png','Site 5.png','Site 6.png','Site 7.png','Site 8.png']

for row, image in enumerate(images):

  img = openpyxl.drawing.image.Image(image)
  img.anchor = 'A'+str(row*30+1)

  ws.add_image(img)

  worksheet.insert_image(30*row,1,image)
  

workbook.close()

wb.save('out.xlsx')

wb.close()

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Anastasia D. Gavanas
Anastasia D. Gavanas
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial