<s> import xlrd <EOL> import datetime <EOL> import math <EOL> from . base import TableParser <EOL> class WorkbookParser ( TableParser ) : <EOL> workbook = None <EOL> worksheet = None <EOL> sheet_name = <NUM_LIT:0> <EOL> start_row = None <EOL> column_count = None <EOL> no_pickle_parser = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> binary = True <EOL> def parse ( self ) : <EOL> self . parse_workbook ( ) <EOL> if self . sheet_name is None : <EOL> self . data = [ { '<STR_LIT:name>' : name , '<STR_LIT:data>' : self . get_sheet_by_name ( name ) } <EOL> for name in self . sheet_names ] <EOL> return <EOL> sheet_name = self . sheet_name <EOL> if isinstance ( self . sheet_name , int ) : <EOL> sheet_name = self . sheet_names [ sheet_name ] <EOL> self . parse_worksheet ( sheet_name ) <EOL> if self . header_row is None : <EOL> if self . start_row is not None : <EOL> self . header_row = self . start_row - <NUM_LIT:1> <EOL> else : <EOL> self . column_count = <NUM_LIT:0> <EOL> def checkval ( cell ) : <EOL> if cell . value is not None and cell . value != '<STR_LIT>' : <EOL> return True <EOL> return False <EOL> search_rows = min ( len ( self . worksheet ) - <NUM_LIT:1> , self . max_header_row ) <EOL> for row in range ( search_rows , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> count = len ( list ( filter ( checkval , self . worksheet [ row ] ) ) ) <EOL> if count >= self . column_count : <EOL> self . column_count = count <EOL> self . header_row = row <EOL> if self . start_row is None : <EOL> self . start_row = self . header_row + <NUM_LIT:1> <EOL> if self . field_names is None : <EOL> rows = self . worksheet [ self . header_row : self . start_row ] <EOL> self . field_names = [ <EOL> str ( c . value ) or '<STR_LIT>' % i for i , c in enumerate ( rows [ <NUM_LIT:0> ] ) <EOL> ] <EOL> for row in rows [ <NUM_LIT:1> : ] : <EOL> for i , c in enumerate ( row ) : <EOL> self . field_names [ i ] += "<STR_LIT:\n>" + str ( c . value ) <EOL> seen_fields = set ( ) <EOL> for i , field in enumerate ( self . field_names ) : <EOL> if field in seen_fields : <EOL> field += str ( i ) <EOL> self . field_names [ i ] = field <EOL> seen_fields . add ( field ) <EOL> self . data = list ( map ( self . parse_row , self . worksheet [ self . start_row : ] ) ) <EOL> self . extra_data = { } <EOL> if self . header_row > <NUM_LIT:0> : <EOL> for r in range ( <NUM_LIT:0> , self . header_row ) : <EOL> for c , cell in enumerate ( self . worksheet [ r ] ) : <EOL> val = self . get_value ( cell ) <EOL> if val is not None and val != '<STR_LIT>' : <EOL> self . extra_data . setdefault ( r , { } ) <EOL> self . extra_data [ r ] [ c ] = val <EOL> def parse_workbook ( self ) : <EOL> raise NotImplementedError <EOL> @ property <EOL> def sheet_names ( self ) : <EOL> raise NotImplementedError <EOL> def get_sheet_by_name ( self , name ) : <EOL> raise NotImplementedError <EOL> def parse_worksheet ( self , name ) : <EOL> raise NotImplementedError <EOL> def parse_row ( self , row ) : <EOL> raise NotImplementedError <EOL> def get_value ( self , cell ) : <EOL> raise NotImplementedError <EOL> def dump ( self , file = None ) : <EOL> if file is None : <EOL> file = self . file <EOL> write , close = self . open_worksheet ( file ) <EOL> for i , field in enumerate ( self . field_names ) : <EOL> write ( <NUM_LIT:0> , i , field ) <EOL> for r , row in enumerate ( self . data ) : <EOL> for c , field in enumerate ( self . field_names ) : <EOL> write ( r + <NUM_LIT:1> , c , row [ field ] ) <EOL> close ( ) <EOL> class ExcelParser ( WorkbookParser ) : <EOL> def parse_workbook ( self ) : <EOL> self . workbook = xlrd . open_workbook ( file_contents = self . file . read ( ) ) <EOL> @ property <EOL> def sheet_names ( self ) : <EOL> return self . workbook . sheet_names ( ) <EOL> def get_sheet_by_name ( self , name ) : <EOL> return self . workbook . sheet_by_name ( name ) <EOL> def parse_worksheet ( self , name ) : <EOL> worksheet = self . get_sheet_by_name ( name ) <EOL> self . worksheet = [ worksheet . row ( i ) for i in range ( worksheet . nrows ) ] <EOL> def parse_row ( self , row ) : <EOL> return { name : self . get_value ( row [ i ] ) <EOL> for i , name in enumerate ( self . get_field_names ( ) ) <EOL> if i < len ( row ) } <EOL> def get_value ( self , cell ) : <EOL> if cell . ctype == xlrd . XL_CELL_DATE : <EOL> time , date = math . modf ( cell . value ) <EOL> tpl = xlrd . xldate_as_tuple ( cell . value , self . workbook . datemode ) <EOL> if date and time : <EOL> return datetime . datetime ( * tpl ) <EOL> elif date : <EOL> return datetime . date ( * tpl [ <NUM_LIT:0> : <NUM_LIT:3> ] ) <EOL> else : <EOL> return datetime . time ( * tpl [ <NUM_LIT:3> : <NUM_LIT:6> ] ) <EOL> return cell . value <EOL> def open_worksheet ( self , file ) : <EOL> if getattr ( self , '<STR_LIT:filename>' , '<STR_LIT>' ) . endswith ( '<STR_LIT>' ) : <EOL> import xlwt <EOL> workbook = xlwt . Workbook ( ) <EOL> worksheet = workbook . add_sheet ( '<STR_LIT>' ) <EOL> def close ( ) : <EOL> workbook . save ( file ) <EOL> else : <EOL> import xlsxwriter <EOL> workbook = xlsxwriter . Workbook ( file ) <EOL> worksheet = workbook . add_worksheet ( ) <EOL> def close ( ) : <EOL> workbook . close ( ) <EOL> return worksheet . write , close </s>
