How to replace images path in xlsx with original images using python

Viewed 17

I have an excel file where one field has an image path. below is the sample excel file

sample excel

I have images in img folder. How to replace the image column with original images using python(pandas or HTML method or any other method)?

1 Answers

If all the files are .png, you could include something like this (assuming img/... is a valid folder path:

import xlwings as xw
wb = xw.Book("files.xlsx")
ws = wb.sheets("sheet_name")

# loop through cells in the column, expanding downwards from cell E2
for i, cell in enumerate(ws.range("E2").options(expand="vertical").value):
    # add pictures using the file path, and paste starting in column F, same row
    ws.pictures.add(cell+".png", anchor=ws.range("F"+str(i+2)))

This appends ".png" to the end of the file path.

You can change the size too, see the documentation.

Related