I currently need to replace some text: "hello" with "hi" wherever it may appear in an xlsx file(s) [think search and replace].
Following the logic from Find and replace in cells from excel in python, I see that I am able to successfully open the source xlsx file, and then finally save to a new xlsx file, however my text never gets replaced.
Note: my text may appear in the beginning, middle, or end of a string and the cell in which it appears may vary from one xlsx file to the next.
Here is my code currently:
wb = openpyxl.load_workbook(sourcefile.xlsx')
wb.sheetnames
sheet = wb["sheet1"]
amountOfRows = sheet.max_row
amountOfColumns = sheet.max_column
for i in range(amountOfColumns):
for k in range(amountOfRows):
cell = str(sheet[get_column_letter(i+1)+str(k+1)].value)
if( str(cell[0]) == "hello"):
newCell = "hi"+cell[1:]
sheet[get_column_letter(i+1)+str(k+1)]=newCell
wb.save('targetFile.xlsx')
Any idea where I am messing this up? Any guidance would be greatly appreciated!