在 sheet 中的任何单元格中使用 python openpyxl 查找并替换 xlsx 文件中的文本
Find and Replace text in xlsx file with python openpyxl in whichever cell it appear in within sheet
我目前需要将一些文本:"hello" 替换为 "hi" 可能出现在 xlsx 文件中的任何位置 [考虑搜索和替换]。
按照 中的逻辑,我看到我能够成功打开源 xlsx 文件,然后最终保存到一个新的 xlsx 文件,但是我的文本从未被替换。
注意:我的文本可能出现在字符串的开头、中间或结尾,并且它出现的单元格可能因一个 xlsx 文件而异。
这是我目前的代码:
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')
知道我哪里搞砸了吗?任何指导将不胜感激!
使用in
关键字和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))
如果您想要不区分大小写 search/replace 或更复杂的匹配,您可以使用 regular expression。添加 import #re
并使用
if s != None and re.search('hello',s,flags=re.I):
ws.cell(r,c).value = re.sub('hello',"Hi",s,flags=re.I)
感谢您提供此解决方案。此外,当单元格上的值之一是整数而不是字符串时,我尝试实施类似的解决方案时 运行 遇到了问题。解决方法是使用 s = str(ws.cell(r,c).value)
例如:
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))
我目前需要将一些文本:"hello" 替换为 "hi" 可能出现在 xlsx 文件中的任何位置 [考虑搜索和替换]。
按照
注意:我的文本可能出现在字符串的开头、中间或结尾,并且它出现的单元格可能因一个 xlsx 文件而异。
这是我目前的代码:
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')
知道我哪里搞砸了吗?任何指导将不胜感激!
使用in
关键字和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))
如果您想要不区分大小写 search/replace 或更复杂的匹配,您可以使用 regular expression。添加 import #re
并使用
if s != None and re.search('hello',s,flags=re.I):
ws.cell(r,c).value = re.sub('hello',"Hi",s,flags=re.I)
感谢您提供此解决方案。此外,当单元格上的值之一是整数而不是字符串时,我尝试实施类似的解决方案时 运行 遇到了问题。解决方法是使用 s = str(ws.cell(r,c).value)
例如:
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))