使用openpyxl操作excel去除所有单元格回车和换行
我这里使用的是openpyxl,需要安装pip install openpyxl
直接上代码
from openpyxl import load_workbookfile = "C:\\xxx\\1.xlsx" #要处理的文件路径wb = load_workbook(file) #加载文件ws = wb.activefor i in range(1,ws.max_row+1):for j in range(1,ws.max_column+1):old = ws.cell(i, j).valueif old is not None:ws.cell(i, j).value = old.strip().replace(' ', '').replace("\n", "")wb.save(file)wb.close()print("处理完成")