Skip to content
Advertisement

How to replace images path in xlsx with original images using python [closed]

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)?

can we use it with file:///[file_name] to get the image in browser and I see with HTML we get the images in excel how to implement with local file.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement