Highlight an Excel Cell Using Python
- import xlrd, xlwt
- inputBook = xlrd.open_workbook(‘test.xls’)
- inputSheet = inputBook.sheet_by_index(0)
- outputBook = xlwt.Workbook()
- outputSheet = outputBook.add_sheet(“sheet 1”, cell_overwrite_ok=True)
- style = xlwt.easyxf(‘pattern: pattern solid, fore-colour red’)
- for x in range(inputSheet.nrows):
- for y in range(inputSheet.ncols):
- if(inputSheet.cell(x,y).value):
- cellValue = inputSheet.cell(x,y).value
- outputSheet.write(x,y, cellValue)
- outputSheet.write(5,5, “rewritten cell”, style)
- outputBook.save(“test.xls”)
This code opens a excel file, and recopies all the contents into a new excel file, and allows you to add highlighted cells to it. Good for validation purposes.