Package camelot :: Package camelot :: Package view :: Package export :: Module excel
[hide private]
[frames] | no frames]

Source Code for Module camelot.camelot.view.export.excel

  1  #  ================================================================================== 
  2  # 
  3  #  Copyright (C) 2007-2008 Conceptive Engineering bvba. All rights reserved. 
  4  #  www.conceptive.be / project-camelot@conceptive.be 
  5  # 
  6  #  This file is part of the Camelot Library. 
  7  # 
  8  #  This file may be used under the terms of the GNU General Public 
  9  #  License version 2.0 as published by the Free Software Foundation 
 10  #  and appearing in the file LICENSE.GPL included in the packaging of 
 11  #  this file.  Please review the following information to ensure GNU 
 12  #  General Public Licensing requirements will be met: 
 13  #  http://www.trolltech.com/products/qt/opensource.html 
 14  # 
 15  #  If you are unsure which license is appropriate for your use, please 
 16  #  review the following information: 
 17  #  http://www.trolltech.com/products/qt/licensing.html or contact 
 18  #  project-camelot@conceptive.be. 
 19  # 
 20  #  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 
 21  #  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 
 22  # 
 23  #  For use of this library in commercial applications, please contact 
 24  #  project-camelot@conceptive.be 
 25  # 
 26  #  ================================================================================== 
 27   
 28  from pyExcelerator import * 
 29  import pickle 
 30  import time 
 31  import datetime 
 32   
 33  titleFont = Font()              # initializing titleFont Object 
 34  headerFont = Font()             # initializing headerFont Object 
 35  cellFont = Font()               # initializing cellFont Object 
 36   
 37  titleFont.name = 'Arial'        # Setting Fonts Name 
 38  headerFont.name = 'Arial' 
 39  cellFont.name = 'Arial' 
 40   
 41  titleFont.bold = True           # Setting title font to bold 
 42  headerFont.bold = True          # Setting column header font to bold 
 43  cellFont.bold = False           # Setting cell font to bold 
 44   
 45  titleFont.height = 240          # 12*20 = 240 Font Size 
 46  headerFont.height = 220         # 10*20 = 240 Font Size 
 47  cellFont.height = 220           # 10*20 = 240 Font Size 
 48   
 49  brdLeft = Borders()                # Defining border which is around header 
 50  brdLeft.left = 0x01 
 51   
 52  brdRight = Borders()                # Defining border which is around header 
 53  brdRight.right = 0x01 
 54   
 55  brdTop = Borders()                # Defining border which is around header 
 56  brdTop.top = 0x01 
 57   
 58  brdBottom = Borders()                # Defining border which is around header 
 59  brdBottom.bottom = 0x01 
 60   
 61  brdTopLeft = Borders() 
 62  brdTopLeft.top = 0x01 
 63  brdTopLeft.left = 0x01 
 64   
 65  brdBottomLeft = Borders() 
 66  brdBottomLeft.bottom = 0x01 
 67  brdBottomLeft.left = 0x01 
 68   
 69  brdBottomRight = Borders() 
 70  brdBottomRight.bottom = 0x01 
 71  brdBottomRight.right = 0x01 
 72   
 73  brdTopRight = Borders() 
 74  brdTopRight.top = 0x01 
 75  brdTopRight.right = 0x01 
 76   
 77  dateStyle = XFStyle() 
 78   
 79   
 80  titleStyle = XFStyle() 
 81  headerStyle = XFStyle() 
 82  cellStyle = XFStyle() 
 83  dateStyle = XFStyle() 
 84   
 85  leftCellStyle = XFStyle() 
 86  rightCellStyle = XFStyle() 
 87  bottomCellStyle = XFStyle() 
 88  topleftCellStyle = XFStyle() 
 89  bottomleftCellStyle = XFStyle() 
 90  bottomrightCellStyle = XFStyle() 
 91  toprightCellStyle = XFStyle() 
 92   
 93  titleStyle.font = titleFont 
 94  headerStyle.font = headerFont 
 95  headerStyle.borders = brdTop 
 96  cellStyle.font = cellFont 
 97   
 98  topleftCellStyle.font = headerFont 
 99  topleftCellStyle.borders = brdTopLeft 
100   
101  bottomleftCellStyle.font = cellFont 
102  bottomleftCellStyle.borders = brdBottomLeft 
103   
104  bottomrightCellStyle.font = cellFont 
105  bottomrightCellStyle.borders = brdBottomRight 
106   
107  toprightCellStyle.font = headerFont 
108  toprightCellStyle.borders = brdTopRight 
109   
110  leftCellStyle.borders = brdLeft 
111  leftCellStyle.font = cellFont 
112   
113  rightCellStyle.borders = brdRight 
114  leftCellStyle.font = cellFont 
115   
116  bottomCellStyle.borders = brdBottom 
117  bottomCellStyle.font = cellFont 
118   
119  pat1 = Pattern() 
120  pat1.pattern = Pattern.SOLID_PATTERN 
121  pat1.pattern_fore_colour = 0x16 
122  headerStyle.pattern = pat1 
123  topleftCellStyle.pattern = pat1 
124  toprightCellStyle.pattern = pat1 
125   
126 -class ExcelExport:
127 - def exportToFile(self, filename, title, headerList, dataList):
128 """ 129 @param filename: the file to which to save the exported excel 130 @param title: title to put in the first row of the genarated excel file 131 @param headerList: list of header definitions 132 @param dataList: list or generator with the row data 133 """ 134 w = Workbook() 135 ws = w.add_sheet('Sheet1') 136 ## Writing Title 137 ws.write(0, 0, title, titleStyle) # Writing Title 138 ws.col(0).width = len(title) * 400 # Setting cell width 139 ## Writing Header 140 myDataTypeDict = {} # dictionary of datatype, {columnnumber, Datatype} 141 myPrecisionDict = {} # dictionary of precision , {columnnumber, Precision} 142 myLengthDict = {} # dictionary of length , {columnnumber, length} 143 myFormatDict = {} # dictionary of dateformat , {columnnumber, format} 144 for n,desc in enumerate(headerList): 145 lst = desc[1] 146 if n==0: 147 ws.write(2, n, lst['name'], topleftCellStyle) 148 elif n==len(headerList)-1: 149 ws.write(2, n, lst['name'], toprightCellStyle) 150 else: 151 ws.write(2, n, lst['name'], headerStyle) 152 if len(lst['name']) < 8: 153 ws.col(n).width = 8 * 375 154 else: 155 ws.col(n).width = len(lst['name']) * 375 156 myDataTypeDict[ n ] = lst["python_type"] 157 if lst["python_type"] == float: 158 myPrecisionDict [ n ] = lst["precision"] #Populating precision dictionary 159 elif lst["python_type"] == datetime.date: 160 myFormatDict [ n ] = lst["format"] #Populating date Format dictionary 161 elif lst["python_type"] == str: 162 if 'length' in lst: 163 myLengthDict [ n ] = lst["length"] #Populating Column Length dictionary 164 ## Writing Data 165 row = 3 166 column = 0 167 valueAddedInSize = 0 168 formatStr = '0' 169 for dictCounter in dataList: # iterating the dataList, having dictionary 170 column = 0 171 cellStyle.num_format_str = '0' 172 for i in range( 0 , len(dictCounter)): #for i in dictCounter: 173 valueAddedInSize = 0 174 val = dictCounter[i] 175 if val != None: 176 if not isinstance(val,(str,unicode,int,float,datetime.datetime,datetime.time,datetime.date, 177 ExcelFormula.Formula) ): 178 val = unicode(val) 179 if myDataTypeDict.has_key(column) == True: 180 if myLengthDict.get(column) != None: 181 if len(val) > myLengthDict[ column ]: 182 val = val[0:myLengthDict[ column ]] 183 elif myDataTypeDict[ column ] == str: 184 formatStr = '0' 185 elif myDataTypeDict[ column ] == int: 186 formatStr = '0' 187 elif myDataTypeDict[ column ] == float: 188 formatStr = '0.' 189 for j in range( 0 , myPrecisionDict[ column ]): 190 formatStr += '0' 191 valueAddedInSize = len(formatStr) # To fit the cell width + 1 (of dot(.)) 192 elif myDataTypeDict[ column ] == datetime.date: 193 formatStr = myFormatDict[column] 194 val = datetime.datetime( day = val.day, year = val.year, month = val.month) 195 elif myDataTypeDict[ column ] == bool: 196 formatStr = '0' 197 else: 198 formatStr = '0' 199 cellStyle.num_format_str = formatStr 200 bottomCellStyle.num_format_str = formatStr 201 rightCellStyle.num_format_str = formatStr 202 bottomrightCellStyle.num_format_str = formatStr 203 leftCellStyle.num_format_str = formatStr 204 bottomleftCellStyle.num_format_str = formatStr 205 elif val == None: 206 val = ' ' 207 if row - 2 == len(dataList): 208 #we re at the bottom row 209 if i==0: 210 ws.write(row , column, val , bottomleftCellStyle) 211 elif i == len(dictCounter)-1: 212 ws.write(row , column, val , bottomrightCellStyle) 213 else: 214 ws.write(row , column, val , bottomCellStyle) 215 else: 216 if i==0: 217 ws.write(row , column, val , leftCellStyle) 218 elif i + 1 == len(dictCounter) and row - 2 != len(dataList): #right column 219 ws.write(row , column, val , rightCellStyle) 220 else: 221 ws.write(row , column, val , cellStyle) 222 if ws.col(column).width < (len(unicode( val )) )* 300: 223 ws.col(column).width = (len(unicode( val )) + valueAddedInSize )* 300 224 column = column + 1 225 row = row + 1 226 w.save(filename)
227 228 229 if __name__ == '__main__': 230 arguments = {} 231 arguments = pickle.load(open('Purchase order.pickle', 'rb')) 232 233 dataList = list(arguments['data']) 234 headerList = list(arguments['columns']) 235 title = arguments['title'] 236 237 objExcel = clsExcel() 238 objExcel.createExcel( title, headerList, dataList ) 239