-1

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.

rahul
  • 3
  • 5

1 Answers1

0

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.

Rawson
  • 2,637
  • 1
  • 5
  • 14
  • import xlwings as xw ws = xw.Book('files.xlsx') # 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))) Can you send me the full code please @Rawson – rahul Sep 24 '22 at 20:38
  • I have added the `ws` definition - it is just the sheet of the workbook. – Rawson Sep 24 '22 at 20:53
  • File "d:\work\thum_file\.venv\lib\site-packages\xlwings\_xlwindows.py", line 530, in __init__ self._xl = COMRetryObjectWrapper(DispatchEx("Excel.Application")) File "d:\work\thum_file\.venv\lib\site-packages\win32com\client\__init__.py", line 145, in DispatchEx dispatch = pythoncom.CoCreateInstanceEx( pywintypes.com_error: (-2147221005, 'Invalid class string', None, None) Getting the following error – rahul Sep 25 '22 at 04:22
  • Which part of the code do you receive that error message for? Are you able to run the first few lines, and `ws.range("E2").options(expand="vertical").value`? If so, it is an error for `ws.pictures...`. – Rawson Sep 25 '22 at 11:55
  • wb = xw.Book("files.xlsx") at this part itself I getting the error and I don't have ms office installed on my computer I see it depended on excel from this I understand DispatchEx("Excel.Application")) – rahul Sep 25 '22 at 12:29
  • Oh, right. You can't use xlwings without Excel, see the answer [to this question](https://stackoverflow.com/questions/30767813/does-xlwings-require-an-excel-install) by the developer. – Rawson Sep 26 '22 at 19:55
  • Can you please send me the code in pandas or others that is not depeneded on excel – rahul Sep 27 '22 at 05:40