from openpyxl import load_workbook
def partial_replace_in_xlsx(input_file, output_file, sheet_name=None,
old_substr=None, new_substr=None):
wb = load_workbook(input_file)
if sheet_name:
ws = wb[sheet_name]
else:
ws = wb.active
for row in ws.iter_rows():
for cell in row:
if isinstance(cell.value, str) and old_substr in cell.value:
cell.value = cell.value.replace(old_substr, new_substr)
print(f"已修改单元格 {cell.coordinate}: '{cell.value}'")
wb.save(output_file)
print(f"修改后的文件已保存为 '{output_file}'")
if __name__ == "__main__":
input_xlsx = r'E:\Pro\书\pptx\月饼\超级工具定制\界址点成果表.xlsx' # 输入文件
output_xlsx = r'E:\Pro\书\pptx\月饼\超级工具定制\mo.xlsx' # 输出文件
old_text = "[面积]" # 要替换的子字符串
new_text = "new_text" # 新的子字符串
partial_replace_in_xlsx(input_xlsx, output_xlsx, old_substr=old_text, new_substr=new_text)