如何根据条件替换Excel文件中的文本?



我试图在已经存在的excel表中替换文本时发现关键字,在附上的示例中,我能够获得值&单元格坐标,但单元格位置格式不正确,不能与sheetobj一起使用来替换值,我如何才能获得正确的单元格坐标

import openpyxl
path = "./BoldDemo.xlsx"
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active
for values, rows in zip(sheet_obj.values, sheet_obj.rows):
# print(values, rows)
if "Hello" in values:
print(rows)
sheet_obj[rows] = "Hello test This is a Replace Text" # need asistance here
wb_obj.save(path)

请检查此代码。

import openpyxl
path = "./test.xlsx"
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active
rows = sheet_obj.max_row
cols = sheet_obj.max_column
for i in range(1, rows + 1):
for j in range(1, cols + 1):
if "Hello" in str(sheet_obj.cell(i, j).value):
print(sheet_obj.cell(i, j).coordinate)
sheet_obj.cell(i, j, value="Hello test This is a Replace Text")
wb_obj.save(path)

最新更新