Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying Excel Sheet cell color code using XLRD package

Tags:

python

excel

xlrd

I am writing a python script to read data from an excel sheet using xlrd. Few of the cells of the the work sheet are highlighted with different color and I want to identify the color code of the cell. Is there any way to do that ? An example would be really appreciated.

like image 536
Kinjal Avatar asked Nov 03 '11 06:11

Kinjal


People also ask

What is xlrd package?

xlrd is a module that allows Python to read data from Excel files.


1 Answers

Here is one way to handle this:

import xlrd book = xlrd.open_workbook("sample.xls", formatting_info=True) sheets = book.sheet_names() print "sheets are:", sheets for index, sh in enumerate(sheets):     sheet = book.sheet_by_index(index)     print "Sheet:", sheet.name     rows, cols = sheet.nrows, sheet.ncols     print "Number of rows: %s   Number of cols: %s" % (rows, cols)     for row in range(rows):         for col in range(cols):             print "row, col is:", row+1, col+1,             thecell = sheet.cell(row, col)                   # could get 'dump', 'value', 'xf_index'             print thecell.value,             xfx = sheet.cell_xf_index(row, col)             xf = book.xf_list[xfx]             bgx = xf.background.pattern_colour_index             print bgx 

More info on the Python-Excel Google Group.

like image 52
JMax Avatar answered Sep 28 '22 08:09

JMax