简体   繁体   中英

Find and Replace text in xlsx file with python openpyxl in whichever cell it appear in within sheet

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!

Use in keyword and replace method

import openpyxl

wb = openpyxl.load_workbook("sourcefile.xlsx")
ws = wb["sheet1"]

i = 0
for r in range(1,ws.max_row+1):
    for c in range(1,ws.max_column+1):
        s = ws.cell(r,c).value
        if s != None and "hello" in s: 
            ws.cell(r,c).value = s.replace("hello","hi") 

            print("row {} col {} : {}".format(r,c,s))
            i += 1

wb.save('targetfile.xlsx')
print("{} cells updated".format(i))

If you want case insensitive search/replace or more complicated matching you can use a regular expression . Add import #re and use

if s != None and re.search('hello',s,flags=re.I): 
    ws.cell(r,c).value = re.sub('hello',"Hi",s,flags=re.I)

Thanks for this solution. Also, I ran into issue when trying to implement similar solution when one of the values on the cell was integer instead of string. The fix to it was the using s = str(ws.cell(r,c).value)

eg:

import openpyxl

wb = openpyxl.load_workbook("sourcefile.xlsx")
ws = wb["sheet1"]

i = 0
for r in range(1,ws.max_row+1):
    for c in range(1,ws.max_column+1):
        s = str(ws.cell(r,c).value)
        if s != None and "hello" in s: 
            ws.cell(r,c).value = s.replace("hello","hi") 

            print("row {} col {} : {}".format(r,c,s))
            i += 1

wb.save('targetfile.xlsx')
print("{} cells updated".format(i))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM