Shawn's Sketch Blog

Month

July 2011

1 post

Highlight an Excel Cell Using Python
  1. import xlrd, xlwt
  2. inputBook = xlrd.open_workbook(‘test.xls’)
  3. inputSheet = inputBook.sheet_by_index(0)
  4. outputBook = xlwt.Workbook()
  5. outputSheet = outputBook.add_sheet(“sheet 1”, cell_overwrite_ok=True)
  6. style = xlwt.easyxf(‘pattern: pattern solid, fore-colour red’)
  7. for x in range(inputSheet.nrows):
  8.     for y in range(inputSheet.ncols):
  9.         if(inputSheet.cell(x,y).value):
  10.            cellValue = inputSheet.cell(x,y).value
  11.            outputSheet.write(x,y, cellValue)
  12. outputSheet.write(5,5, “rewritten cell”, style)
  13. 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.

    Jul 22, 201111 notes
    #python #excel #highlight
    Next page →
    2012 2013
    • January 2
    • February 2
    • March 2
    • April 1
    • May 2
    • June
    • July
    • August
    • September
    • October
    • November
    • December
    2011 2012 2013
    • January 2
    • February 1
    • March 3
    • April 1
    • May
    • June 1
    • July
    • August
    • September
    • October
    • November
    • December 1
    2011 2012
    • January 7
    • February 18
    • March 69
    • April 9
    • May 20
    • June 1
    • July 1
    • August 5
    • September 1
    • October 4
    • November 2
    • December 2