Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

xlrd

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
like image 880
Jason Bellino Avatar asked Jul 18 '11 16:07

Jason Bellino


1 Answers

My solution here was based on a class written by 'timmorgan' which can be found here. The class requires that the excel document you wish to act upon be open. You then create the excel document object and then call the 'get_range' method which returns a range object. This range object can then be used to get at font properties of the cell specified.

#--Requires excel document to be open
import pyexcel
book = pyexcel.ExcelDocument(visible=True) #--keeps excel open
cell = 'r171'
r = book.get_range(cell)
val = book.get_value(cell)

print val, r.font.italic, r.font.name
like image 151
Jason Bellino Avatar answered Nov 15 '22 07:11

Jason Bellino