Search code examples
pythonxlrd

Using XLRD module and Python to determine cell font style (italics or not)


I'm trying to parse data in an excel spreadsheet using XLRD to determine which cell values are italicized. This information will be used to set a flag as to whether the value is an estimated or reported value. Below is an example of the data:

owner_name          year    Jan     Feb     Mar     Apr     May     Jun     Jul     Aug     Sep     Oct     Nov     Dec
Alachua, city of    1978    17.4    15.7    16.7    18.3    18.9    18.9    19.2    17.4    19.5    19.8    17.1    16.4
Archer, city of     1978    5.6      3.6     4.3     4.5     4.7     4.8     5.3     5.3     5.4     5.6     3.9     2.8

I have not used XLRD to any great extent, aside from playing around with some of the basic functions to get a feel for how to pull data from the spreadsheet. Now I need to add that extra bit of functionality to identify italicized cell values.

Thanks in advance for your help...

EDIT: XLRD provided me with the functionality I need; thanks to John Machin for the answer. Here is teh codez:

import xlrd

book = xlrd.open_workbook('fl_data.xls',formatting_info=True)
sh = book.sheet_by_index(0)

for row in range(0,sh.nrows):
    font = book.font_list
    cell_val = sh.cell_value(row,1)
    cell_xf = book.xf_list[sh.cell_xf_index(row,1)]

    print cell_val,font[cell_xf.font_index].italic

Solution

  • Using xlrd (by itself, not with pyexcel):

    Here is a link to a topic to the python-excel google-group. It's about getting font colour but that gets you 99% of the way.