Re: Getting a particular cell background color index

3,387 views
Skip to first unread message
Message has been deleted

John Machin

unread,
Sep 7, 2007, 10:55:51 PM9/7/07
to python-excel

On Sep 8, 1:19 am, JS <joseph.ste...@medtronic.com> wrote:
> I'm trying to get each cells background color index value for each
> worksheet, for the ranges of cells in each worksheet, but having
> difficulty.
>
> import xlrd
> book = xlrd.open_workbook("test.xls", formatting_info=1)
> sheets = book.sheet_names()
> print "sheets are:", sheets
> for index, sh in enumerate(sheets):
> sheet = book.sheet_by_index(index)
> rows, cols = sheet.nrows, sheet.ncols
> xfx = sheet.cell_xf_index(row, col)
> xf = book.xf_list[xfx]
> bg = xf.background
> bgx = bg.background_colour_index
>
> I get values back but not the one's expected for the each cell in each
> sheet.
> What am I doing wrong?

1. Not posting the actual code that you ran.
2. Not telling us what results you got.
3. Not telling us what results you expected, and the basis for your
expectation.

JS

unread,
Sep 10, 2007, 7:36:56 AM9/10/07
to python-excel
It works fine.
Here's how to do it for anyone who needs it.
Thanks, John for pointing out some missing rows in the original post.

import xlrd
book = xlrd.open_workbook("sample.xls", formatting_info=1)


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
>>>
sheets are: [u'Sheet1', u'Sheet2', u'Sheet3']
Sheet: Sheet1
Number of rows: 6 Number of cols: 2
row, col is: 1 1 53
row, col is: 1 2 53
row, col is: 2 1 64
row, col is: 2 2 64
row, col is: 3 1 64
row, col is: 3 2 64
row, col is: 4 1 64
row, col is: 4 2 64
row, col is: 5 1 64
row, col is: 5 2 64
row, col is: 6 1 34
row, col is: 6 2 34
Sheet: Sheet2
Number of rows: 0 Number of cols: 0
Sheet: Sheet3
Number of rows: 0 Number of cols: 0
>>>

> expectation.- Hide quoted text -
>
> - Show quoted text -

Reply all
Reply to author
Forward
0 new messages