Total Complexity | 96 |
Total Lines | 907 |
Duplicated Lines | 16.87 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like excelexporters.spacesaving often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import base64 |
||
2 | import uuid |
||
3 | import os |
||
4 | from openpyxl.chart import ( |
||
5 | PieChart, |
||
6 | LineChart, |
||
7 | BarChart, |
||
8 | Reference, |
||
9 | ) |
||
10 | from openpyxl.styles import PatternFill, Border, Side, Alignment, Font |
||
11 | from openpyxl.drawing.image import Image |
||
12 | from openpyxl import Workbook |
||
13 | from openpyxl.chart.label import DataLabelList |
||
14 | import openpyxl.utils.cell as format_cell |
||
15 | |||
16 | |||
17 | #################################################################################################################### |
||
18 | # PROCEDURES |
||
19 | # Step 1: Validate the report data |
||
20 | # Step 2: Generate excel file |
||
21 | # Step 3: Encode the excel file bytes to Base64 |
||
22 | #################################################################################################################### |
||
23 | |||
24 | |||
25 | View Code Duplication | def export(report, |
|
|
|||
26 | name, |
||
27 | reporting_start_datetime_local, |
||
28 | reporting_end_datetime_local, |
||
29 | period_type): |
||
30 | #################################################################################################################### |
||
31 | # Step 1: Validate the report data |
||
32 | #################################################################################################################### |
||
33 | if report is None: |
||
34 | return None |
||
35 | print(report) |
||
36 | |||
37 | #################################################################################################################### |
||
38 | # Step 2: Generate excel file from the report data |
||
39 | #################################################################################################################### |
||
40 | filename = generate_excel(report, |
||
41 | name, |
||
42 | reporting_start_datetime_local, |
||
43 | reporting_end_datetime_local, |
||
44 | period_type) |
||
45 | #################################################################################################################### |
||
46 | # Step 3: Encode the excel file to Base64 |
||
47 | #################################################################################################################### |
||
48 | try: |
||
49 | with open(filename, 'rb') as binary_file: |
||
50 | binary_file_data = binary_file.read() |
||
51 | except IOError as ex: |
||
52 | pass |
||
53 | |||
54 | # Base64 encode the bytes |
||
55 | base64_encoded_data = base64.b64encode(binary_file_data) |
||
56 | # get the Base64 encoded data using human-readable characters. |
||
57 | base64_message = base64_encoded_data.decode('utf-8') |
||
58 | # delete the file from server |
||
59 | try: |
||
60 | os.remove(filename) |
||
61 | except NotImplementedError as ex: |
||
62 | pass |
||
63 | return base64_message |
||
64 | |||
65 | |||
66 | def generate_excel(report, |
||
67 | name, |
||
68 | reporting_start_datetime_local, |
||
69 | reporting_end_datetime_local, |
||
70 | period_type): |
||
71 | wb = Workbook() |
||
72 | ws = wb.active |
||
73 | |||
74 | # Row height |
||
75 | ws.row_dimensions[1].height = 102 |
||
76 | for i in range(2, 2000 + 1): |
||
77 | ws.row_dimensions[i].height = 42 |
||
78 | |||
79 | # Col width |
||
80 | ws.column_dimensions['A'].width = 1.5 |
||
81 | |||
82 | ws.column_dimensions['B'].width = 25.0 |
||
83 | |||
84 | for i in range(ord('C'), ord('L')): |
||
85 | ws.column_dimensions[chr(i)].width = 15.0 |
||
86 | |||
87 | # Font |
||
88 | name_font = Font(name='Constantia', size=15, bold=True) |
||
89 | title_font = Font(name='宋体', size=15, bold=True) |
||
90 | data_font = Font(name='Franklin Gothic Book', size=11) |
||
91 | |||
92 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
||
93 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
||
94 | right=Side(border_style='medium', color='00000000'), |
||
95 | bottom=Side(border_style='medium', color='00000000'), |
||
96 | top=Side(border_style='medium', color='00000000') |
||
97 | ) |
||
98 | b_border = Border( |
||
99 | bottom=Side(border_style='medium', color='00000000'), |
||
100 | ) |
||
101 | |||
102 | b_c_alignment = Alignment(vertical='bottom', |
||
103 | horizontal='center', |
||
104 | text_rotation=0, |
||
105 | wrap_text=True, |
||
106 | shrink_to_fit=False, |
||
107 | indent=0) |
||
108 | c_c_alignment = Alignment(vertical='center', |
||
109 | horizontal='center', |
||
110 | text_rotation=0, |
||
111 | wrap_text=True, |
||
112 | shrink_to_fit=False, |
||
113 | indent=0) |
||
114 | b_r_alignment = Alignment(vertical='bottom', |
||
115 | horizontal='right', |
||
116 | text_rotation=0, |
||
117 | wrap_text=True, |
||
118 | shrink_to_fit=False, |
||
119 | indent=0) |
||
120 | c_r_alignment = Alignment(vertical='bottom', |
||
121 | horizontal='center', |
||
122 | text_rotation=0, |
||
123 | wrap_text=True, |
||
124 | shrink_to_fit=False, |
||
125 | indent=0) |
||
126 | |||
127 | # Img |
||
128 | img = Image("excelexporters/myems.png") |
||
129 | img.width = img.width * 0.85 |
||
130 | img.height = img.height * 0.85 |
||
131 | # img = Image("myems.png") |
||
132 | ws.add_image(img, 'B1') |
||
133 | |||
134 | # Title |
||
135 | ws.row_dimensions[3].height = 60 |
||
136 | |||
137 | ws['B3'].font = name_font |
||
138 | ws['B3'].alignment = b_r_alignment |
||
139 | ws['B3'] = 'Name:' |
||
140 | ws['C3'].border = b_border |
||
141 | ws['C3'].alignment = b_c_alignment |
||
142 | ws['C3'].font = name_font |
||
143 | ws['C3'] = name |
||
144 | |||
145 | ws['D3'].font = name_font |
||
146 | ws['D3'].alignment = b_r_alignment |
||
147 | ws['D3'] = 'Period:' |
||
148 | ws['E3'].border = b_border |
||
149 | ws['E3'].alignment = b_c_alignment |
||
150 | ws['E3'].font = name_font |
||
151 | ws['E3'] = period_type |
||
152 | |||
153 | ws['F3'].font = name_font |
||
154 | ws['F3'].alignment = b_r_alignment |
||
155 | ws['F3'] = 'Date:' |
||
156 | ws['G3'].border = b_border |
||
157 | ws['G3'].alignment = b_c_alignment |
||
158 | ws['G3'].font = name_font |
||
159 | ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
||
160 | ws.merge_cells("G3:H3") |
||
161 | |||
162 | if "reporting_period" not in report.keys() or \ |
||
163 | "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
||
164 | filename = str(uuid.uuid4()) + '.xlsx' |
||
165 | wb.save(filename) |
||
166 | |||
167 | return filename |
||
168 | |||
169 | ################################## |
||
170 | |||
171 | current_row_number = 6 |
||
172 | |||
173 | reporting_period_data = report['reporting_period'] |
||
174 | |||
175 | has_names_data_flag = True |
||
176 | |||
177 | if "names" not in reporting_period_data.keys() or \ |
||
178 | reporting_period_data['names'] is None or \ |
||
179 | len(reporting_period_data['names']) == 0: |
||
180 | has_names_data_flag = False |
||
181 | |||
182 | if has_names_data_flag: |
||
183 | ws['B' + str(current_row_number)].font = title_font |
||
184 | ws['B' + str(current_row_number)] = name + ' 报告期节约' |
||
185 | |||
186 | current_row_number += 1 |
||
187 | |||
188 | category = reporting_period_data['names'] |
||
189 | ca_len = len(category) |
||
190 | |||
191 | ws.row_dimensions[current_row_number].height = 75 |
||
192 | ws['B' + str(current_row_number)].fill = table_fill |
||
193 | ws['B' + str(current_row_number)].border = f_border |
||
194 | |||
195 | col = 'C' |
||
196 | |||
197 | for i in range(0, ca_len): |
||
198 | ws[col + str(current_row_number)].fill = table_fill |
||
199 | ws[col + str(current_row_number)].font = name_font |
||
200 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
201 | ws[col + str(current_row_number)].border = f_border |
||
202 | ws[col + str(current_row_number)] = \ |
||
203 | reporting_period_data['names'][i] + " (基线-实际) (" + reporting_period_data['units'][i] + ")" |
||
204 | |||
205 | col = chr(ord(col) + 1) |
||
206 | |||
207 | ws[col + str(current_row_number)].fill = table_fill |
||
208 | ws[col + str(current_row_number)].font = name_font |
||
209 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
210 | ws[col + str(current_row_number)].border = f_border |
||
211 | ws[col + str(current_row_number)] = '吨标准煤 (基线-实际) (TCE)' |
||
212 | |||
213 | col = chr(ord(col) + 1) |
||
214 | |||
215 | ws[col + str(current_row_number)].fill = table_fill |
||
216 | ws[col + str(current_row_number)].font = name_font |
||
217 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
218 | ws[col + str(current_row_number)].border = f_border |
||
219 | ws[col + str(current_row_number)] = '吨二氧化碳排放 (基线-实际) (TCO2E)' |
||
220 | |||
221 | col = chr(ord(col) + 1) |
||
222 | |||
223 | current_row_number += 1 |
||
224 | |||
225 | ws['B' + str(current_row_number)].font = title_font |
||
226 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
227 | ws['B' + str(current_row_number)].border = f_border |
||
228 | ws['B' + str(current_row_number)] = '节约' |
||
229 | |||
230 | col = 'C' |
||
231 | |||
232 | for i in range(0, ca_len): |
||
233 | ws[col + str(current_row_number)].font = name_font |
||
234 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
235 | ws[col + str(current_row_number)].border = f_border |
||
236 | ws[col + str(current_row_number)] = round(reporting_period_data['subtotals_saving'][i], 2) |
||
237 | |||
238 | col = chr(ord(col) + 1) |
||
239 | |||
240 | ws[col + str(current_row_number)].font = name_font |
||
241 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
242 | ws[col + str(current_row_number)].border = f_border |
||
243 | ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgce_saving'] / 1000, 2) |
||
244 | |||
245 | col = chr(ord(col) + 1) |
||
246 | |||
247 | ws[col + str(current_row_number)].font = name_font |
||
248 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
249 | ws[col + str(current_row_number)].border = f_border |
||
250 | ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgco2e_saving'] / 1000, 2) |
||
251 | |||
252 | col = chr(ord(col) + 1) |
||
253 | |||
254 | current_row_number += 1 |
||
255 | |||
256 | ws['B' + str(current_row_number)].font = title_font |
||
257 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
258 | ws['B' + str(current_row_number)].border = f_border |
||
259 | ws['B' + str(current_row_number)] = '单位面积值' |
||
260 | |||
261 | col = 'C' |
||
262 | |||
263 | for i in range(0, ca_len): |
||
264 | ws[col + str(current_row_number)].font = name_font |
||
265 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
266 | ws[col + str(current_row_number)].border = f_border |
||
267 | ws[col + str(current_row_number)] = round(reporting_period_data['subtotals_per_unit_area_saving'][i], 2) |
||
268 | |||
269 | col = chr(ord(col) + 1) |
||
270 | |||
271 | ws[col + str(current_row_number)].font = name_font |
||
272 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
273 | ws[col + str(current_row_number)].border = f_border |
||
274 | ws[col + str(current_row_number)] = round(reporting_period_data['total_in_kgce_per_unit_area_saving'] / 1000, 2) |
||
275 | |||
276 | col = chr(ord(col) + 1) |
||
277 | |||
278 | ws[col + str(current_row_number)].font = name_font |
||
279 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
280 | ws[col + str(current_row_number)].border = f_border |
||
281 | ws[col + str(current_row_number)] = \ |
||
282 | round(reporting_period_data['total_in_kgco2e_per_unit_area_saving'] / 1000, 2) |
||
283 | |||
284 | col = chr(ord(col) + 1) |
||
285 | |||
286 | current_row_number += 1 |
||
287 | |||
288 | ws['B' + str(current_row_number)].font = title_font |
||
289 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
290 | ws['B' + str(current_row_number)].border = f_border |
||
291 | ws['B' + str(current_row_number)] = '环比' |
||
292 | |||
293 | col = 'C' |
||
294 | |||
295 | for i in range(0, ca_len): |
||
296 | ws[col + str(current_row_number)].font = name_font |
||
297 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
298 | ws[col + str(current_row_number)].border = f_border |
||
299 | ws[col + str(current_row_number)] = str( |
||
300 | round(reporting_period_data['increment_rates_saving'][i] * 100, 2)) + '%' \ |
||
301 | if reporting_period_data['increment_rates_saving'][i] is not None else '-' |
||
302 | |||
303 | col = chr(ord(col) + 1) |
||
304 | |||
305 | ws[col + str(current_row_number)].font = name_font |
||
306 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
307 | ws[col + str(current_row_number)].border = f_border |
||
308 | ws[col + str(current_row_number)] = str( |
||
309 | round(reporting_period_data['increment_rate_in_kgce_saving'] * 100, 2)) + '%' \ |
||
310 | if reporting_period_data['increment_rate_in_kgce_saving'] is not None else '-' |
||
311 | |||
312 | col = chr(ord(col) + 1) |
||
313 | |||
314 | ws[col + str(current_row_number)].font = name_font |
||
315 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
316 | ws[col + str(current_row_number)].border = f_border |
||
317 | ws[col + str(current_row_number)] = str( |
||
318 | round(reporting_period_data['increment_rate_in_kgco2e_saving'] * 100, 2)) + '%' \ |
||
319 | if reporting_period_data['increment_rate_in_kgco2e_saving'] is not None else '-' |
||
320 | |||
321 | col = chr(ord(col) + 1) |
||
322 | |||
323 | current_row_number += 2 |
||
324 | |||
325 | ws['B' + str(current_row_number)].font = title_font |
||
326 | ws['B' + str(current_row_number)] = name + ' 吨标准煤(TCE)占比' |
||
327 | |||
328 | current_row_number += 1 |
||
329 | table_start_row_number = current_row_number |
||
330 | chart_start_row_number = current_row_number |
||
331 | |||
332 | ws.row_dimensions[current_row_number].height = 60 |
||
333 | ws['B' + str(current_row_number)].fill = table_fill |
||
334 | ws['B' + str(current_row_number)].border = f_border |
||
335 | |||
336 | ws['C' + str(current_row_number)].fill = table_fill |
||
337 | ws['C' + str(current_row_number)].font = name_font |
||
338 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
339 | ws['C' + str(current_row_number)].border = f_border |
||
340 | ws['C' + str(current_row_number)] = '节约' |
||
341 | |||
342 | ws['D' + str(current_row_number)].fill = table_fill |
||
343 | ws['D' + str(current_row_number)].font = name_font |
||
344 | ws['D' + str(current_row_number)].alignment = c_c_alignment |
||
345 | ws['D' + str(current_row_number)].border = f_border |
||
346 | ws['D' + str(current_row_number)] = '吨标准煤(TCE) 节约占比' |
||
347 | |||
348 | current_row_number += 1 |
||
349 | |||
350 | subtotals_in_kgce_saving_sum = sum_list(reporting_period_data['subtotals_in_kgce_saving']) |
||
351 | |||
352 | for i in range(0, ca_len): |
||
353 | ws['B' + str(current_row_number)].font = title_font |
||
354 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
355 | ws['B' + str(current_row_number)].border = f_border |
||
356 | ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
||
357 | |||
358 | ws['C' + str(current_row_number)].font = name_font |
||
359 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
360 | ws['C' + str(current_row_number)].border = f_border |
||
361 | ws['C' + str(current_row_number)] = round(reporting_period_data['subtotals_in_kgce_saving'][i] / 1000, 3) |
||
362 | |||
363 | ws['D' + str(current_row_number)].font = name_font |
||
364 | ws['D' + str(current_row_number)].alignment = c_c_alignment |
||
365 | ws['D' + str(current_row_number)].border = f_border |
||
366 | ws['D' + str(current_row_number)] = str(round(reporting_period_data['subtotals_in_kgce_saving'][i] / |
||
367 | subtotals_in_kgce_saving_sum * 100, 2)) + '%'\ |
||
368 | if abs(subtotals_in_kgce_saving_sum) > 0 else '-' |
||
369 | |||
370 | current_row_number += 1 |
||
371 | |||
372 | table_end_row_number = current_row_number - 1 |
||
373 | |||
374 | if ca_len < 4: |
||
375 | current_row_number = current_row_number - ca_len + 4 |
||
376 | |||
377 | current_row_number += 1 |
||
378 | |||
379 | pie = PieChart() |
||
380 | pie.title = name + ' 吨标准煤(TCE)占比' |
||
381 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
382 | pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
||
383 | pie.add_data(pie_data, titles_from_data=True) |
||
384 | pie.set_categories(labels) |
||
385 | pie.height = 7.25 |
||
386 | pie.width = 9 |
||
387 | s1 = pie.series[0] |
||
388 | s1.dLbls = DataLabelList() |
||
389 | s1.dLbls.showCatName = False |
||
390 | s1.dLbls.showVal = True |
||
391 | s1.dLbls.showPercent = True |
||
392 | ws.add_chart(pie, 'E' + str(chart_start_row_number)) |
||
393 | |||
394 | ws['B' + str(current_row_number)].font = title_font |
||
395 | ws['B' + str(current_row_number)] = name + ' 吨二氧化碳排放(TCO2E)占比' |
||
396 | |||
397 | current_row_number += 1 |
||
398 | table_start_row_number = current_row_number |
||
399 | chart_start_row_number = current_row_number |
||
400 | |||
401 | ws.row_dimensions[current_row_number].height = 60 |
||
402 | ws['B' + str(current_row_number)].fill = table_fill |
||
403 | ws['B' + str(current_row_number)].border = f_border |
||
404 | |||
405 | ws['C' + str(current_row_number)].fill = table_fill |
||
406 | ws['C' + str(current_row_number)].font = name_font |
||
407 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
408 | ws['C' + str(current_row_number)].border = f_border |
||
409 | ws['C' + str(current_row_number)] = '节约' |
||
410 | |||
411 | ws['D' + str(current_row_number)].fill = table_fill |
||
412 | ws['D' + str(current_row_number)].font = name_font |
||
413 | ws['D' + str(current_row_number)].alignment = c_c_alignment |
||
414 | ws['D' + str(current_row_number)].border = f_border |
||
415 | ws['D' + str(current_row_number)] = '吨二氧化碳排放(TCO2E) 节约占比' |
||
416 | |||
417 | current_row_number += 1 |
||
418 | |||
419 | subtotals_in_kgco2e_saving_sum = sum_list(reporting_period_data['subtotals_in_kgco2e_saving']) |
||
420 | |||
421 | for i in range(0, ca_len): |
||
422 | ws['B' + str(current_row_number)].font = title_font |
||
423 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
424 | ws['B' + str(current_row_number)].border = f_border |
||
425 | ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
||
426 | |||
427 | ws['C' + str(current_row_number)].font = name_font |
||
428 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
429 | ws['C' + str(current_row_number)].border = f_border |
||
430 | ws['C' + str(current_row_number)] = round(reporting_period_data['subtotals_in_kgco2e_saving'][i] / 1000, 3) |
||
431 | |||
432 | ws['D' + str(current_row_number)].font = name_font |
||
433 | ws['D' + str(current_row_number)].alignment = c_c_alignment |
||
434 | ws['D' + str(current_row_number)].border = f_border |
||
435 | ws['D' + str(current_row_number)] = str(round(reporting_period_data['subtotals_in_kgco2e_saving'][i] / |
||
436 | subtotals_in_kgco2e_saving_sum * 100, 2)) + '%'\ |
||
437 | if abs(subtotals_in_kgco2e_saving_sum) > 0 else '-' |
||
438 | |||
439 | current_row_number += 1 |
||
440 | |||
441 | table_end_row_number = current_row_number - 1 |
||
442 | |||
443 | if ca_len < 4: |
||
444 | current_row_number = current_row_number - ca_len + 4 |
||
445 | |||
446 | current_row_number += 1 |
||
447 | |||
448 | pie = PieChart() |
||
449 | pie.title = name + ' 吨二氧化碳排放(TCO2E)占比' |
||
450 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
451 | pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
||
452 | pie.add_data(pie_data, titles_from_data=True) |
||
453 | pie.set_categories(labels) |
||
454 | pie.height = 7.25 |
||
455 | pie.width = 9 |
||
456 | s1 = pie.series[0] |
||
457 | s1.dLbls = DataLabelList() |
||
458 | s1.dLbls.showCatName = False |
||
459 | s1.dLbls.showVal = True |
||
460 | s1.dLbls.showPercent = True |
||
461 | ws.add_chart(pie, 'E' + str(chart_start_row_number)) |
||
462 | |||
463 | ################################ |
||
464 | |||
465 | has_values_saving_data = True |
||
466 | has_timestamps_data = True |
||
467 | |||
468 | if 'values_saving' not in reporting_period_data.keys() or \ |
||
469 | reporting_period_data['values_saving'] is None or \ |
||
470 | len(reporting_period_data['values_saving']) == 0: |
||
471 | has_values_saving_data = False |
||
472 | |||
473 | if 'timestamps' not in reporting_period_data.keys() or \ |
||
474 | reporting_period_data['timestamps'] is None or \ |
||
475 | len(reporting_period_data['timestamps']) == 0 or \ |
||
476 | len(reporting_period_data['timestamps'][0]) == 0: |
||
477 | has_timestamps_data = False |
||
478 | |||
479 | View Code Duplication | if has_values_saving_data and has_timestamps_data: |
|
480 | ca_len = len(reporting_period_data['names']) |
||
481 | time = reporting_period_data['timestamps'][0] |
||
482 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
||
483 | ws['B' + str(current_row_number)].font = title_font |
||
484 | ws['B' + str(current_row_number)] = name + ' 详细数据' |
||
485 | |||
486 | current_row_number += 1 |
||
487 | |||
488 | chart_start_row_number = current_row_number |
||
489 | |||
490 | current_row_number += ca_len * 6 + real_timestamps_len * 7 + 1 |
||
491 | table_start_row_number = current_row_number |
||
492 | |||
493 | ws.row_dimensions[current_row_number].height = 60 |
||
494 | ws['B' + str(current_row_number)].fill = table_fill |
||
495 | ws['B' + str(current_row_number)].font = title_font |
||
496 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
497 | ws['B' + str(current_row_number)].border = f_border |
||
498 | ws['B' + str(current_row_number)] = '日期时间' |
||
499 | |||
500 | col = 'C' |
||
501 | |||
502 | for i in range(0, ca_len): |
||
503 | ws[col + str(current_row_number)].fill = table_fill |
||
504 | ws[col + str(current_row_number)].font = title_font |
||
505 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
506 | ws[col + str(current_row_number)].border = f_border |
||
507 | ws[col + str(current_row_number)] = \ |
||
508 | reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
509 | col = chr(ord(col) + 1) |
||
510 | |||
511 | current_row_number += 1 |
||
512 | |||
513 | for i in range(0, len(time)): |
||
514 | ws['B' + str(current_row_number)].font = title_font |
||
515 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
516 | ws['B' + str(current_row_number)].border = f_border |
||
517 | ws['B' + str(current_row_number)] = time[i] |
||
518 | |||
519 | col = 'C' |
||
520 | for j in range(0, ca_len): |
||
521 | ws[col + str(current_row_number)].font = title_font |
||
522 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
523 | ws[col + str(current_row_number)].border = f_border |
||
524 | ws[col + str(current_row_number)] = round(reporting_period_data['values_saving'][j][i], 2) \ |
||
525 | if reporting_period_data['values_saving'][j][i] is not None else 0.00 |
||
526 | col = chr(ord(col) + 1) |
||
527 | |||
528 | current_row_number += 1 |
||
529 | |||
530 | table_end_row_number = current_row_number - 1 |
||
531 | |||
532 | ws['B' + str(current_row_number)].font = title_font |
||
533 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
534 | ws['B' + str(current_row_number)].border = f_border |
||
535 | ws['B' + str(current_row_number)] = '小计' |
||
536 | |||
537 | col = 'C' |
||
538 | |||
539 | for i in range(0, ca_len): |
||
540 | ws[col + str(current_row_number)].font = title_font |
||
541 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
542 | ws[col + str(current_row_number)].border = f_border |
||
543 | ws[col + str(current_row_number)] = round(reporting_period_data['subtotals_saving'][i], 2) |
||
544 | col = chr(ord(col) + 1) |
||
545 | |||
546 | current_row_number += 2 |
||
547 | |||
548 | format_time_width_number = 1.0 |
||
549 | min_len_number = 1.0 |
||
550 | min_width_number = 11.0 # format_time_width_number * min_len_number + 4 and min_width_number > 11.0 |
||
551 | |||
552 | if period_type == 'hourly': |
||
553 | format_time_width_number = 4.0 |
||
554 | min_len_number = 2 |
||
555 | min_width_number = 12.0 |
||
556 | elif period_type == 'daily': |
||
557 | format_time_width_number = 2.5 |
||
558 | min_len_number = 4 |
||
559 | min_width_number = 14.0 |
||
560 | elif period_type == 'monthly': |
||
561 | format_time_width_number = 2.1 |
||
562 | min_len_number = 4 |
||
563 | min_width_number = 12.4 |
||
564 | elif period_type == 'yearly': |
||
565 | format_time_width_number = 1.5 |
||
566 | min_len_number = 5 |
||
567 | min_width_number = 11.5 |
||
568 | |||
569 | for i in range(0, ca_len): |
||
570 | line = LineChart() |
||
571 | line.title = '报告期节约 - ' + \ |
||
572 | reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
573 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
574 | line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number) |
||
575 | line.add_data(line_data, titles_from_data=True) |
||
576 | line.set_categories(labels) |
||
577 | line_data = line.series[0] |
||
578 | line_data.marker.symbol = "circle" |
||
579 | line_data.smooth = True |
||
580 | line.height = 8.25 |
||
581 | line.width = format_time_width_number * len(time) if len(time) > min_len_number else min_width_number |
||
582 | if line.width > 24: |
||
583 | line.width = 24 |
||
584 | line.x_axis.crosses = 'min' |
||
585 | line.dLbls = DataLabelList() |
||
586 | line.dLbls.dLblPos = 't' |
||
587 | line.dLbls.showVal = True |
||
588 | line.dLbls.showPercent = False |
||
589 | chart_col = 'B' |
||
590 | chart_cell = chart_col + str(chart_start_row_number) |
||
591 | chart_start_row_number += 6 |
||
592 | ws.add_chart(line, chart_cell) |
||
593 | |||
594 | ############################################# |
||
595 | |||
596 | has_child_space_data_flag = True |
||
597 | |||
598 | if "child_space" not in report.keys() or "energy_category_names" not in report['child_space'].keys() or \ |
||
599 | len(report['child_space']["energy_category_names"]) == 0 \ |
||
600 | or 'child_space_names_array' not in report['child_space'].keys() \ |
||
601 | or report['child_space']['energy_category_names'] is None \ |
||
602 | or len(report['child_space']['child_space_names_array']) == 0 \ |
||
603 | or len(report['child_space']['child_space_names_array'][0]) == 0: |
||
604 | has_child_space_data_flag = False |
||
605 | |||
606 | if has_child_space_data_flag: |
||
607 | child_space_data = report['child_space'] |
||
608 | ca_len = len(child_space_data['energy_category_names']) |
||
609 | |||
610 | ws['B' + str(current_row_number)].font = title_font |
||
611 | ws['B' + str(current_row_number)] = name + ' 子空间数据' |
||
612 | |||
613 | current_row_number += 1 |
||
614 | table_start_row_number = current_row_number |
||
615 | |||
616 | ws.row_dimensions[current_row_number].height = 60 |
||
617 | ws['B' + str(current_row_number)].fill = table_fill |
||
618 | ws['B' + str(current_row_number)].font = name_font |
||
619 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
620 | ws['B' + str(current_row_number)].border = f_border |
||
621 | ws['B' + str(current_row_number)] = '子空间' |
||
622 | |||
623 | col = 'C' |
||
624 | |||
625 | for i in range(0, ca_len): |
||
626 | ws[col + str(current_row_number)].fill = table_fill |
||
627 | ws[col + str(current_row_number)].font = name_font |
||
628 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
629 | ws[col + str(current_row_number)].border = f_border |
||
630 | ws[col + str(current_row_number)] = \ |
||
631 | child_space_data['energy_category_names'][i] + " (" + child_space_data['units'][i] + ")" |
||
632 | col = chr(ord(col) + 1) |
||
633 | |||
634 | current_row_number += 1 |
||
635 | ca_child_len = len(child_space_data['child_space_names_array'][0]) |
||
636 | |||
637 | for i in range(0, ca_child_len): |
||
638 | ws['B' + str(current_row_number)].font = title_font |
||
639 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
640 | ws['B' + str(current_row_number)].border = f_border |
||
641 | ws['B' + str(current_row_number)] = child_space_data['child_space_names_array'][0][i] |
||
642 | current_row_number += 1 |
||
643 | |||
644 | current_row_number -= ca_child_len |
||
645 | |||
646 | for i in range(0, ca_child_len): |
||
647 | col = 'C' |
||
648 | for j in range(0, ca_len): |
||
649 | ws[col + str(current_row_number)].font = name_font |
||
650 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
651 | ws[col + str(current_row_number)].border = f_border |
||
652 | ws[col + str(current_row_number)] = round(child_space_data['subtotals_saving_array'][j][i], 2) |
||
653 | col = chr(ord(col) + 1) |
||
654 | |||
655 | current_row_number += 1 |
||
656 | |||
657 | table_end_row_number = current_row_number - 1 |
||
658 | |||
659 | col = 'B' |
||
660 | |||
661 | for i in range(0, ca_len): |
||
662 | pie = PieChart() |
||
663 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
664 | pie_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number) |
||
665 | pie.add_data(pie_data, titles_from_data=True) |
||
666 | pie.set_categories(labels) |
||
667 | pie.title = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
668 | pie.height = 6.6 |
||
669 | pie.width = 9 |
||
670 | s1 = pie.series[0] |
||
671 | s1.dLbls = DataLabelList() |
||
672 | s1.dLbls.showCatName = False |
||
673 | s1.dLbls.showVal = True |
||
674 | s1.dLbls.showPercent = True |
||
675 | chart_cell = '' |
||
676 | if i % 2 == 0: |
||
677 | chart_cell = 'B' + str(current_row_number) |
||
678 | else: |
||
679 | chart_cell = 'E' + str(current_row_number) |
||
680 | current_row_number += 5 |
||
681 | ws.add_chart(pie, chart_cell) |
||
682 | |||
683 | if ca_len % 2 == 1: |
||
684 | current_row_number += 5 |
||
685 | |||
686 | current_row_number += 1 |
||
687 | ########################################## |
||
688 | current_sheet_parameters_row_number = chart_start_row_number + 1 |
||
689 | has_parameters_names_and_timestamps_and_values_data = True |
||
690 | if 'parameters' not in report.keys() or \ |
||
691 | report['parameters'] is None or \ |
||
692 | 'names' not in report['parameters'].keys() or \ |
||
693 | report['parameters']['names'] is None or \ |
||
694 | len(report['parameters']['names']) == 0 or \ |
||
695 | 'timestamps' not in report['parameters'].keys() or \ |
||
696 | report['parameters']['timestamps'] is None or \ |
||
697 | len(report['parameters']['timestamps']) == 0 or \ |
||
698 | 'values' not in report['parameters'].keys() or \ |
||
699 | report['parameters']['values'] is None or \ |
||
700 | len(report['parameters']['values']) == 0 or \ |
||
701 | timestamps_data_all_equal_0(report['parameters']['timestamps']): |
||
702 | has_parameters_names_and_timestamps_and_values_data = False |
||
703 | if has_parameters_names_and_timestamps_and_values_data: |
||
704 | |||
705 | ############################### |
||
706 | # new worksheet |
||
707 | ############################### |
||
708 | |||
709 | parameters_data = report['parameters'] |
||
710 | parameters_names_len = len(parameters_data['names']) |
||
711 | |||
712 | parameters_ws = wb.create_sheet('相关参数') |
||
713 | |||
714 | parameters_timestamps_data_max_len = \ |
||
715 | get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps'])) |
||
716 | |||
717 | # Row height |
||
718 | parameters_ws.row_dimensions[1].height = 102 |
||
719 | for i in range(2, 7 + 1): |
||
720 | parameters_ws.row_dimensions[i].height = 42 |
||
721 | |||
722 | for i in range(8, parameters_timestamps_data_max_len + 10): |
||
723 | parameters_ws.row_dimensions[i].height = 60 |
||
724 | |||
725 | # Col width |
||
726 | parameters_ws.column_dimensions['A'].width = 1.5 |
||
727 | |||
728 | parameters_ws.column_dimensions['B'].width = 25.0 |
||
729 | |||
730 | for i in range(3, 12 + parameters_names_len * 3): |
||
731 | parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0 |
||
732 | |||
733 | # Img |
||
734 | img = Image("excelexporters/myems.png") |
||
735 | img.width = img.width * 0.85 |
||
736 | img.height = img.height * 0.85 |
||
737 | # img = Image("myems.png") |
||
738 | parameters_ws.add_image(img, 'B1') |
||
739 | |||
740 | # Title |
||
741 | parameters_ws.row_dimensions[3].height = 60 |
||
742 | |||
743 | parameters_ws['B3'].font = name_font |
||
744 | parameters_ws['B3'].alignment = b_r_alignment |
||
745 | parameters_ws['B3'] = 'Name:' |
||
746 | parameters_ws['C3'].border = b_border |
||
747 | parameters_ws['C3'].alignment = b_c_alignment |
||
748 | parameters_ws['C3'].font = name_font |
||
749 | parameters_ws['C3'] = name |
||
750 | |||
751 | parameters_ws['D3'].font = name_font |
||
752 | parameters_ws['D3'].alignment = b_r_alignment |
||
753 | parameters_ws['D3'] = 'Period:' |
||
754 | parameters_ws['E3'].border = b_border |
||
755 | parameters_ws['E3'].alignment = b_c_alignment |
||
756 | parameters_ws['E3'].font = name_font |
||
757 | parameters_ws['E3'] = period_type |
||
758 | |||
759 | parameters_ws['F3'].font = name_font |
||
760 | parameters_ws['F3'].alignment = b_r_alignment |
||
761 | parameters_ws['F3'] = 'Date:' |
||
762 | parameters_ws['G3'].border = b_border |
||
763 | parameters_ws['G3'].alignment = b_c_alignment |
||
764 | parameters_ws['G3'].font = name_font |
||
765 | parameters_ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
||
766 | parameters_ws.merge_cells("G3:H3") |
||
767 | |||
768 | parameters_ws_current_row_number = 6 |
||
769 | |||
770 | parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font |
||
771 | parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' 相关参数' |
||
772 | |||
773 | parameters_ws_current_row_number += 1 |
||
774 | |||
775 | parameters_table_start_row_number = parameters_ws_current_row_number |
||
776 | |||
777 | parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
||
778 | |||
779 | parameters_ws_current_row_number += 1 |
||
780 | |||
781 | table_current_col_number = 'B' |
||
782 | |||
783 | for i in range(0, parameters_names_len): |
||
784 | |||
785 | if len(parameters_data['timestamps'][i]) == 0: |
||
786 | continue |
||
787 | |||
788 | parameters_ws[table_current_col_number + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
789 | parameters_ws[table_current_col_number + str(parameters_ws_current_row_number - 1)].border = f_border |
||
790 | |||
791 | col = chr(ord(table_current_col_number) + 1) |
||
792 | |||
793 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
794 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
||
795 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].font = name_font |
||
796 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].alignment = c_c_alignment |
||
797 | parameters_ws[col + str(parameters_ws_current_row_number - 1)] = parameters_data['names'][i] |
||
798 | |||
799 | table_current_row_number = parameters_ws_current_row_number |
||
800 | |||
801 | for j, value in enumerate(list(parameters_data['timestamps'][i])): |
||
802 | col = table_current_col_number |
||
803 | |||
804 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
805 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
806 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
807 | parameters_ws[col + str(table_current_row_number)] = value |
||
808 | |||
809 | col = chr(ord(col) + 1) |
||
810 | |||
811 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
812 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
813 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
814 | parameters_ws[col + str(table_current_row_number)] = round(parameters_data['values'][i][j], 2) |
||
815 | |||
816 | table_current_row_number += 1 |
||
817 | |||
818 | table_current_col_number = chr(ord(table_current_col_number) + 3) |
||
819 | |||
820 | ######################################################## |
||
821 | # parameters chart and parameters table |
||
822 | ######################################################## |
||
823 | |||
824 | ws['B' + str(current_sheet_parameters_row_number)].font = title_font |
||
825 | ws['B' + str(current_sheet_parameters_row_number)] = name + ' 相关参数' |
||
826 | |||
827 | current_sheet_parameters_row_number += 1 |
||
828 | |||
829 | chart_start_row_number = current_sheet_parameters_row_number |
||
830 | |||
831 | col_index = 0 |
||
832 | |||
833 | for i in range(0, parameters_names_len): |
||
834 | |||
835 | if len(parameters_data['timestamps'][i]) == 0: |
||
836 | continue |
||
837 | |||
838 | line = LineChart() |
||
839 | data_col = 3 + col_index * 3 |
||
840 | labels_col = 2 + col_index * 3 |
||
841 | col_index += 1 |
||
842 | line.title = '相关参数 - ' + \ |
||
843 | parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value |
||
844 | labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1, |
||
845 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
846 | line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number, |
||
847 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
848 | line.add_data(line_data, titles_from_data=True) |
||
849 | line.set_categories(labels) |
||
850 | line_data = line.series[0] |
||
851 | line_data.marker.symbol = "circle" |
||
852 | line_data.smooth = True |
||
853 | line.x_axis.crosses = 'min' |
||
854 | line.height = 8.25 |
||
855 | line.width = 24 |
||
856 | line.dLbls = DataLabelList() |
||
857 | line.dLbls.dLblPos = 't' |
||
858 | line.dLbls.showVal = False |
||
859 | line.dLbls.showPercent = False |
||
860 | chart_col = 'B' |
||
861 | chart_cell = chart_col + str(chart_start_row_number) |
||
862 | chart_start_row_number += 6 |
||
863 | ws.add_chart(line, chart_cell) |
||
864 | |||
865 | current_sheet_parameters_row_number = chart_start_row_number |
||
866 | |||
867 | current_sheet_parameters_row_number += 1 |
||
868 | ########################################## |
||
869 | filename = str(uuid.uuid4()) + '.xlsx' |
||
870 | wb.save(filename) |
||
871 | |||
872 | return filename |
||
873 | |||
874 | |||
875 | def sum_list(lists): |
||
876 | total = 0 |
||
877 | |||
878 | for i in range(0, len(lists)): |
||
879 | total += lists[i] |
||
880 | |||
881 | return total |
||
882 | |||
883 | |||
884 | def timestamps_data_all_equal_0(lists): |
||
885 | for i, value in enumerate(list(lists)): |
||
886 | if len(value) > 0: |
||
887 | return False |
||
888 | |||
889 | return True |
||
890 | |||
891 | |||
892 | def get_parameters_timestamps_lists_max_len(parameters_timestamps_lists): |
||
893 | max_len = 0 |
||
894 | for i, value in enumerate(list(parameters_timestamps_lists)): |
||
895 | if len(value) > max_len: |
||
896 | max_len = len(value) |
||
897 | |||
898 | return max_len |
||
899 | |||
900 | |||
901 | def timestamps_data_not_equal_0(lists): |
||
902 | number = 0 |
||
903 | for i, value in enumerate(list(lists)): |
||
904 | if len(value) > 0: |
||
905 | number += 1 |
||
906 | return number |
||
907 |