<s> import datetime <EOL> import xlrd <EOL> from django . db . models . fields . files import FieldFile <EOL> class XLSReader ( object ) : <EOL> def __init__ ( self , instance , sheet_name = None , sheet_index = <NUM_LIT:0> , on_demand = True ) : <EOL> if isinstance ( instance . source , FieldFile ) : <EOL> source = instance . source . path <EOL> else : <EOL> source = instance . source <EOL> self . workbook = xlrd . open_workbook ( source , on_demand = on_demand ) <EOL> if sheet_name : <EOL> self . worksheet = self . workbook . sheet_by_name ( instance . Meta . sheet_name ) <EOL> else : <EOL> self . worksheet = self . workbook . sheet_by_index ( sheet_index ) <EOL> @ staticmethod <EOL> def convert_value ( item , workbook ) : <EOL> """<STR_LIT>""" <EOL> if item . ctype == <NUM_LIT:3> : <EOL> return datetime . datetime ( * xlrd . xldate_as_tuple ( item . value , workbook . datemode ) ) <EOL> if item . ctype == <NUM_LIT:2> : <EOL> if item . value % <NUM_LIT:1> == <NUM_LIT:0> : <EOL> return int ( item . value ) <EOL> else : <EOL> return item . value <EOL> return item . value <EOL> def read ( self ) : <EOL> for i in xrange ( <NUM_LIT:0> , self . worksheet . nrows ) : <EOL> values = [ self . convert_value ( cell , self . workbook ) for cell in self . worksheet . row ( i ) ] <EOL> yield values </s>
