1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 from pyExcelerator import *
29 import pickle
30 import time
31 import datetime
32
33 titleFont = Font()
34 headerFont = Font()
35 cellFont = Font()
36
37 titleFont.name = 'Arial'
38 headerFont.name = 'Arial'
39 cellFont.name = 'Arial'
40
41 titleFont.bold = True
42 headerFont.bold = True
43 cellFont.bold = False
44
45 titleFont.height = 240
46 headerFont.height = 220
47 cellFont.height = 220
48
49 brdLeft = Borders()
50 brdLeft.left = 0x01
51
52 brdRight = Borders()
53 brdRight.right = 0x01
54
55 brdTop = Borders()
56 brdTop.top = 0x01
57
58 brdBottom = Borders()
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
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
137 ws.write(0, 0, title, titleStyle)
138 ws.col(0).width = len(title) * 400
139
140 myDataTypeDict = {}
141 myPrecisionDict = {}
142 myLengthDict = {}
143 myFormatDict = {}
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"]
159 elif lst["python_type"] == datetime.date:
160 myFormatDict [ n ] = lst["format"]
161 elif lst["python_type"] == str:
162 if 'length' in lst:
163 myLengthDict [ n ] = lst["length"]
164
165 row = 3
166 column = 0
167 valueAddedInSize = 0
168 formatStr = '0'
169 for dictCounter in dataList:
170 column = 0
171 cellStyle.num_format_str = '0'
172 for i in range( 0 , len(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)
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
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):
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