How to get Excel cell properties in Python

Actually, I am using xlrd module 0.8, but I donโ€™t know how to read cell properties such as background color, font, and whether the cell is locked.

I tried to use

import xlrd
wb = xlrd.open_workbook(...)
sh = wb.sheet_by_index(...)
sh.sh._cell_xf_indexes(2, 2)

An error occurs indicating that formatting information should be set when reading wb, but if I had this parameter, it means it has not yet been implemented.

Is there another module, or how can this module be made to read cell properties?

+5
source share
1 answer

Works for me using xlrd version 0.7.6:

from xlrd import open_workbook

wb = open_workbook('tmp.xls', formatting_info=True)
sheet = wb.sheet_by_name("1")
cell = sheet.cell(6, 0)
print "cell.xf_index is", cell.xf_index
fmt = wb.xf_list[cell.xf_index]
print "type(fmt) is", type(fmt)
print
print "fmt.dump():"
fmt.dump()

fmtis an instance of the XF class; see https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html#formatting.XF-class

dump() . :

cell.xf_index is 497
type(fmt) is <class 'xlrd.formatting.XF'>

fmt.dump():
_alignment_flag: 1
_background_flag: 1
_border_flag: 1
_font_flag: 1
_format_flag: 0
_protection_flag: 0
alignment (XFAlignment object):
    hor_align: 1
    indent_level: 0
    rotation: 0
    shrink_to_fit: 0
    text_direction: 0
    text_wrapped: 0
    vert_align: 2
background (XFBackground object):
    background_colour_index: 64
    fill_pattern: 1
    pattern_colour_index: 17
border (XFBorder object):
    bottom_colour_index: 0
    bottom_line_style: 0
    diag_colour_index: 0
    diag_down: 0
    diag_line_style: 0
    diag_up: 0
    left_colour_index: 0
    left_line_style: 0
    right_colour_index: 0
    right_line_style: 0
    top_colour_index: 56
    top_line_style: 1
font_index: 72
format_key: 0
is_style: 0
lotus_123_prefix: 0
parent_style_index: 0
protection (XFProtection object):
    cell_locked: 1
    formula_hidden: 0
xf_index: 497

wb. , fmt.font_index 72, wb.font_list[72] - Font (https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html#formatting.Font-).

+11

All Articles