Total Complexity | 43 |
Total Lines | 601 |
Duplicated Lines | 40.27 % |
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.shopfloorcost 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 | |||
15 | #################################################################################################################### |
||
16 | # PROCEDURES |
||
17 | # Step 1: Validate the report data |
||
18 | # Step 2: Generate excel file |
||
19 | # Step 3: Encode the excel file bytes to Base64 |
||
20 | #################################################################################################################### |
||
21 | |||
22 | |||
23 | View Code Duplication | def export(report, |
|
|
|||
24 | name, |
||
25 | reporting_start_datetime_local, |
||
26 | reporting_end_datetime_local, |
||
27 | period_type): |
||
28 | #################################################################################################################### |
||
29 | # Step 1: Validate the report data |
||
30 | #################################################################################################################### |
||
31 | if report is None: |
||
32 | return None |
||
33 | print(report) |
||
34 | |||
35 | #################################################################################################################### |
||
36 | # Step 2: Generate excel file from the report data |
||
37 | #################################################################################################################### |
||
38 | filename = generate_excel(report, |
||
39 | name, |
||
40 | reporting_start_datetime_local, |
||
41 | reporting_end_datetime_local, |
||
42 | period_type) |
||
43 | #################################################################################################################### |
||
44 | # Step 3: Encode the excel file to Base64 |
||
45 | #################################################################################################################### |
||
46 | try: |
||
47 | with open(filename, 'rb') as binary_file: |
||
48 | binary_file_data = binary_file.read() |
||
49 | except IOError as ex: |
||
50 | pass |
||
51 | |||
52 | # Base64 encode the bytes |
||
53 | base64_encoded_data = base64.b64encode(binary_file_data) |
||
54 | # get the Base64 encoded data using human-readable characters. |
||
55 | base64_message = base64_encoded_data.decode('utf-8') |
||
56 | # delete the file from server |
||
57 | try: |
||
58 | os.remove(filename) |
||
59 | except NotImplementedError as ex: |
||
60 | pass |
||
61 | return base64_message |
||
62 | |||
63 | |||
64 | def generate_excel(report, |
||
65 | name, |
||
66 | reporting_start_datetime_local, |
||
67 | reporting_end_datetime_local, |
||
68 | period_type): |
||
69 | |||
70 | wb = Workbook() |
||
71 | ws = wb.active |
||
72 | |||
73 | # Row height |
||
74 | ws.row_dimensions[1].height = 102 |
||
75 | for i in range(2, 2000 + 1): |
||
76 | ws.row_dimensions[i].height = 42 |
||
77 | |||
78 | # for i in range(2, 37 + 1): |
||
79 | # ws.row_dimensions[i].height = 30 |
||
80 | # |
||
81 | # for i in range(38, 90 + 1): |
||
82 | # ws.row_dimensions[i].height = 30 |
||
83 | |||
84 | # Col width |
||
85 | ws.column_dimensions['A'].width = 1.5 |
||
86 | |||
87 | ws.column_dimensions['B'].width = 25.0 |
||
88 | |||
89 | for i in range(ord('C'), ord('L')): |
||
90 | ws.column_dimensions[chr(i)].width = 15.0 |
||
91 | |||
92 | # Font |
||
93 | name_font = Font(name='Constantia', size=15, bold=True) |
||
94 | title_font = Font(name='宋体', size=15, bold=True) |
||
95 | data_font = Font(name='Franklin Gothic Book', size=11) |
||
96 | |||
97 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
||
98 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
||
99 | right=Side(border_style='medium', color='00000000'), |
||
100 | bottom=Side(border_style='medium', color='00000000'), |
||
101 | top=Side(border_style='medium', color='00000000') |
||
102 | ) |
||
103 | b_border = Border( |
||
104 | bottom=Side(border_style='medium', color='00000000'), |
||
105 | ) |
||
106 | |||
107 | b_c_alignment = Alignment(vertical='bottom', |
||
108 | horizontal='center', |
||
109 | text_rotation=0, |
||
110 | wrap_text=True, |
||
111 | shrink_to_fit=False, |
||
112 | indent=0) |
||
113 | c_c_alignment = Alignment(vertical='center', |
||
114 | horizontal='center', |
||
115 | text_rotation=0, |
||
116 | wrap_text=True, |
||
117 | shrink_to_fit=False, |
||
118 | indent=0) |
||
119 | b_r_alignment = Alignment(vertical='bottom', |
||
120 | horizontal='right', |
||
121 | text_rotation=0, |
||
122 | wrap_text=True, |
||
123 | shrink_to_fit=False, |
||
124 | indent=0) |
||
125 | c_r_alignment = Alignment(vertical='bottom', |
||
126 | horizontal='center', |
||
127 | text_rotation=0, |
||
128 | wrap_text=True, |
||
129 | shrink_to_fit=False, |
||
130 | indent=0) |
||
131 | |||
132 | # Img |
||
133 | img = Image("excelexporters/myems.png") |
||
134 | img.width = img.width * 0.85 |
||
135 | img.height = img.height * 0.85 |
||
136 | # img = Image("myems.png") |
||
137 | ws.add_image(img, 'B1') |
||
138 | |||
139 | # Title |
||
140 | ws.row_dimensions[3].height = 60 |
||
141 | |||
142 | ws['B3'].font = name_font |
||
143 | ws['B3'].alignment = b_r_alignment |
||
144 | ws['B3'] = 'Name:' |
||
145 | ws['C3'].border = b_border |
||
146 | ws['C3'].alignment = b_c_alignment |
||
147 | ws['C3'].font = name_font |
||
148 | ws['C3'] = name |
||
149 | |||
150 | ws['D3'].font = name_font |
||
151 | ws['D3'].alignment = b_r_alignment |
||
152 | ws['D3'] = 'Period:' |
||
153 | ws['E3'].border = b_border |
||
154 | ws['E3'].alignment = b_c_alignment |
||
155 | ws['E3'].font = name_font |
||
156 | ws['E3'] = period_type |
||
157 | |||
158 | ws['F3'].font = name_font |
||
159 | ws['F3'].alignment = b_r_alignment |
||
160 | ws['F3'] = 'Date:' |
||
161 | ws['G3'].alignment = b_c_alignment |
||
162 | ws['G3'].font = name_font |
||
163 | ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
||
164 | ws.merge_cells("G3:H3") |
||
165 | |||
166 | if "reporting_period" not in report.keys() or \ |
||
167 | "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
||
168 | filename = str(uuid.uuid4()) + '.xlsx' |
||
169 | wb.save(filename) |
||
170 | |||
171 | return filename |
||
172 | |||
173 | ################################################# |
||
174 | |||
175 | reporting_period_data = report['reporting_period'] |
||
176 | |||
177 | has_energy_data_flag = True |
||
178 | if "names" not in reporting_period_data.keys() or \ |
||
179 | reporting_period_data['names'] is None or \ |
||
180 | len(reporting_period_data['names']) == 0: |
||
181 | has_energy_data_flag = False |
||
182 | |||
183 | View Code Duplication | if has_energy_data_flag: |
|
184 | ws['B6'].font = title_font |
||
185 | ws['B6'] = name+' 报告期成本' |
||
186 | |||
187 | category = reporting_period_data['names'] |
||
188 | ca_len = len(category) |
||
189 | |||
190 | ws.row_dimensions[7].height = 60 |
||
191 | ws['B7'].fill = table_fill |
||
192 | ws['B7'].border = f_border |
||
193 | |||
194 | ws['B8'].font = title_font |
||
195 | ws['B8'].alignment = c_c_alignment |
||
196 | ws['B8'] = '成本' |
||
197 | ws['B8'].border = f_border |
||
198 | |||
199 | ws['B9'].font = title_font |
||
200 | ws['B9'].alignment = c_c_alignment |
||
201 | ws['B9'] = '单位面积值' |
||
202 | ws['B9'].border = f_border |
||
203 | |||
204 | ws['B10'].font = title_font |
||
205 | ws['B10'].alignment = c_c_alignment |
||
206 | ws['B10'] = '环比' |
||
207 | ws['B10'].border = f_border |
||
208 | |||
209 | col = 'B' |
||
210 | |||
211 | for i in range(0, ca_len): |
||
212 | col = chr(ord('C') + i) |
||
213 | ws[col + '7'].fill = table_fill |
||
214 | ws[col + '7'].font = name_font |
||
215 | ws[col + '7'].alignment = c_c_alignment |
||
216 | ws[col + '7'] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
217 | ws[col + '7'].border = f_border |
||
218 | |||
219 | ws[col + '8'].font = name_font |
||
220 | ws[col + '8'].alignment = c_c_alignment |
||
221 | ws[col + '8'] = round(reporting_period_data['subtotals'][i], 2) |
||
222 | ws[col + '8'].border = f_border |
||
223 | |||
224 | ws[col + '9'].font = name_font |
||
225 | ws[col + '9'].alignment = c_c_alignment |
||
226 | ws[col + '9'] = round(reporting_period_data['subtotals_per_unit_area'][i], 2) |
||
227 | ws[col + '9'].border = f_border |
||
228 | |||
229 | ws[col + '10'].font = name_font |
||
230 | ws[col + '10'].alignment = c_c_alignment |
||
231 | ws[col + '10'] = str(round(reporting_period_data['increment_rates'][i] * 100, 2)) + "%" \ |
||
232 | if reporting_period_data['increment_rates'][i] is not None else "-" |
||
233 | ws[col + '10'].border = f_border |
||
234 | |||
235 | end_col = chr(ord(col)+1) |
||
236 | ws[end_col + '7'].fill = table_fill |
||
237 | ws[end_col + '7'].font = name_font |
||
238 | ws[end_col + '7'].alignment = c_c_alignment |
||
239 | ws[end_col + '7'] = "总计 (" + reporting_period_data['total_unit'] + ")" |
||
240 | ws[end_col + '7'].border = f_border |
||
241 | |||
242 | ws[end_col + '8'].font = name_font |
||
243 | ws[end_col + '8'].alignment = c_c_alignment |
||
244 | ws[end_col + '8'] = round(reporting_period_data['total'], 2) |
||
245 | ws[end_col + '8'].border = f_border |
||
246 | |||
247 | ws[end_col + '9'].font = name_font |
||
248 | ws[end_col + '9'].alignment = c_c_alignment |
||
249 | ws[end_col + '9'] = round(reporting_period_data['total_per_unit_area'], 2) |
||
250 | ws[end_col + '9'].border = f_border |
||
251 | |||
252 | ws[end_col + '10'].font = name_font |
||
253 | ws[end_col + '10'].alignment = c_c_alignment |
||
254 | ws[end_col + '10'] = str(round(reporting_period_data['total_increment_rate'] * 100, 2)) + "%" \ |
||
255 | if reporting_period_data['total_increment_rate'] is not None else "-" |
||
256 | ws[end_col + '10'].border = f_border |
||
257 | |||
258 | else: |
||
259 | for i in range(6, 10 + 1): |
||
260 | ws.row_dimensions[i].height = 0.1 |
||
261 | |||
262 | ################################################# |
||
263 | |||
264 | has_ele_peak_flag = True |
||
265 | if "toppeaks" not in reporting_period_data.keys() or \ |
||
266 | reporting_period_data['toppeaks'] is None or \ |
||
267 | len(reporting_period_data['toppeaks']) == 0: |
||
268 | has_ele_peak_flag = False |
||
269 | |||
270 | if has_ele_peak_flag: |
||
271 | ws['B12'].font = title_font |
||
272 | ws['B12'] = name+'分时用电成本' |
||
273 | |||
274 | ws['B13'].fill = table_fill |
||
275 | ws['B13'].font = name_font |
||
276 | ws['B13'].alignment = c_c_alignment |
||
277 | ws['B13'].border = f_border |
||
278 | |||
279 | ws['C13'].fill = table_fill |
||
280 | ws['C13'].font = name_font |
||
281 | ws['C13'].alignment = c_c_alignment |
||
282 | ws['C13'].border = f_border |
||
283 | ws['C13'] = '分时用电成本' |
||
284 | |||
285 | ws['D13'].fill = table_fill |
||
286 | ws['D13'].font = name_font |
||
287 | ws['D13'].alignment = c_c_alignment |
||
288 | ws['D13'].border = f_border |
||
289 | ws['D13'] = '分时用电成本占比' |
||
290 | |||
291 | wssum = round(reporting_period_data['toppeaks'][0], 2)+round(reporting_period_data['onpeaks'][0], 2)\ |
||
292 | + round(reporting_period_data['midpeaks'][0], 2)+round(reporting_period_data['offpeaks'][0], 2) |
||
293 | |||
294 | ws['B14'].font = title_font |
||
295 | ws['B14'].alignment = c_c_alignment |
||
296 | ws['B14'] = '尖' |
||
297 | ws['B14'].border = f_border |
||
298 | |||
299 | ws['C14'].font = title_font |
||
300 | ws['C14'].alignment = c_c_alignment |
||
301 | ws['C14'].border = f_border |
||
302 | ws['C14'] = round(reporting_period_data['toppeaks'][0], 2) |
||
303 | |||
304 | ws['D14'].font = title_font |
||
305 | ws['D14'].alignment = c_c_alignment |
||
306 | ws['D14'].border = f_border |
||
307 | ws['D14'] = '{:.2%}'.format(round(reporting_period_data['toppeaks'][0], 2)/wssum) |
||
308 | |||
309 | ws['B15'].font = title_font |
||
310 | ws['B15'].alignment = c_c_alignment |
||
311 | ws['B15'] = '峰' |
||
312 | ws['B15'].border = f_border |
||
313 | |||
314 | ws['C15'].font = title_font |
||
315 | ws['C15'].alignment = c_c_alignment |
||
316 | ws['C15'].border = f_border |
||
317 | ws['C15'] = round(reporting_period_data['onpeaks'][0], 2) |
||
318 | |||
319 | ws['D15'].font = title_font |
||
320 | ws['D15'].alignment = c_c_alignment |
||
321 | ws['D15'].border = f_border |
||
322 | ws['D15'] = '{:.2%}'.format(round(reporting_period_data['onpeaks'][0], 2)/wssum) |
||
323 | |||
324 | ws['B16'].font = title_font |
||
325 | ws['B16'].alignment = c_c_alignment |
||
326 | ws['B16'] = '平' |
||
327 | ws['B16'].border = f_border |
||
328 | |||
329 | ws['C16'].font = title_font |
||
330 | ws['C16'].alignment = c_c_alignment |
||
331 | ws['C16'].border = f_border |
||
332 | ws['C16'] = round(reporting_period_data['midpeaks'][0], 2) |
||
333 | |||
334 | ws['D16'].font = title_font |
||
335 | ws['D16'].alignment = c_c_alignment |
||
336 | ws['D16'].border = f_border |
||
337 | ws['D16'] = '{:.2%}'.format(round(reporting_period_data['midpeaks'][0], 2)/wssum) |
||
338 | |||
339 | ws['B17'].font = title_font |
||
340 | ws['B17'].alignment = c_c_alignment |
||
341 | ws['B17'] = '谷' |
||
342 | ws['B17'].border = f_border |
||
343 | |||
344 | ws['C17'].font = title_font |
||
345 | ws['C17'].alignment = c_c_alignment |
||
346 | ws['C17'].border = f_border |
||
347 | ws['C17'] = round(reporting_period_data['offpeaks'][0], 2) |
||
348 | |||
349 | ws['D17'].font = title_font |
||
350 | ws['D17'].alignment = c_c_alignment |
||
351 | ws['D17'].border = f_border |
||
352 | ws['D17'] = '{:.2%}'.format(round(reporting_period_data['offpeaks'][0], 2)/wssum) |
||
353 | |||
354 | pie = PieChart() |
||
355 | pie.title = name+'分时用电成本' |
||
356 | labels = Reference(ws, min_col=2, min_row=14, max_row=17) |
||
357 | pie_data = Reference(ws, min_col=3, min_row=13, max_row=17) |
||
358 | pie.add_data(pie_data, titles_from_data=True) |
||
359 | pie.set_categories(labels) |
||
360 | pie.height = 6.6 |
||
361 | pie.width = 9 |
||
362 | s1 = pie.series[0] |
||
363 | s1.dLbls = DataLabelList() |
||
364 | s1.dLbls.showCatName = False |
||
365 | s1.dLbls.showVal = True |
||
366 | s1.dLbls.showPercent = True |
||
367 | ws.add_chart(pie, "E13") |
||
368 | |||
369 | else: |
||
370 | for i in range(12, 18 + 1): |
||
371 | ws.row_dimensions[i].height = 0.1 |
||
372 | |||
373 | ################################################ |
||
374 | |||
375 | current_row_number = 19 |
||
376 | |||
377 | has_subtotals_data_flag = True |
||
378 | if "subtotals" not in reporting_period_data.keys() or \ |
||
379 | reporting_period_data['subtotals'] is None or \ |
||
380 | len(reporting_period_data['subtotals']) == 0: |
||
381 | has_subtotals_data_flag = False |
||
382 | |||
383 | if has_subtotals_data_flag: |
||
384 | ws['B' + str(current_row_number)].font = title_font |
||
385 | ws['B' + str(current_row_number)] = name + ' 成本占比' |
||
386 | |||
387 | current_row_number += 1 |
||
388 | |||
389 | table_start_row_number = current_row_number |
||
390 | |||
391 | ws['B' + str(current_row_number)].fill = table_fill |
||
392 | ws['B' + str(current_row_number)].font = name_font |
||
393 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
394 | ws['B' + str(current_row_number)].border = f_border |
||
395 | |||
396 | ws['C' + str(current_row_number)].fill = table_fill |
||
397 | ws['C' + str(current_row_number)].font = name_font |
||
398 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
399 | ws['C' + str(current_row_number)].border = f_border |
||
400 | ws['C' + str(current_row_number)] = '成本' |
||
401 | |||
402 | ws['D' + str(current_row_number)].fill = table_fill |
||
403 | ws['D' + str(current_row_number)].font = name_font |
||
404 | ws['D' + str(current_row_number)].alignment = c_c_alignment |
||
405 | ws['D' + str(current_row_number)].border = f_border |
||
406 | ws['D' + str(current_row_number)] = '成本占比' |
||
407 | |||
408 | current_row_number += 1 |
||
409 | |||
410 | ca_len = len(reporting_period_data['names']) |
||
411 | wssum = 0 |
||
412 | for i in range(0, ca_len): |
||
413 | wssum = round(reporting_period_data['subtotals'][i], 2) + wssum |
||
414 | for i in range(0, ca_len): |
||
415 | ws['B' + str(current_row_number)].font = title_font |
||
416 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
417 | ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
||
418 | ws['B' + str(current_row_number)].border = f_border |
||
419 | |||
420 | ws['C' + str(current_row_number)].font = title_font |
||
421 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
422 | ws['C' + str(current_row_number)].border = f_border |
||
423 | ws['C' + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2) |
||
424 | |||
425 | ws['D' + str(current_row_number)].font = title_font |
||
426 | ws['D' + str(current_row_number)].alignment = c_c_alignment |
||
427 | ws['D' + str(current_row_number)].border = f_border |
||
428 | ws['D' + str(current_row_number)] = '{:.2%}'.format(round(reporting_period_data['subtotals'][i], 2) / wssum) |
||
429 | |||
430 | current_row_number += 1 |
||
431 | |||
432 | table_end_row_number = current_row_number - 1 |
||
433 | |||
434 | pie = PieChart() |
||
435 | pie.title = name + ' 成本占比' |
||
436 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
437 | pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
||
438 | pie.add_data(pie_data, titles_from_data=True) |
||
439 | pie.set_categories(labels) |
||
440 | pie.height = 6.6 |
||
441 | pie.width = 9 |
||
442 | s1 = pie.series[0] |
||
443 | s1.dLbls = DataLabelList() |
||
444 | s1.dLbls.showCatName = False |
||
445 | s1.dLbls.showVal = True |
||
446 | s1.dLbls.showPercent = True |
||
447 | table_cell = 'E' + str(table_start_row_number) |
||
448 | ws.add_chart(pie, table_cell) |
||
449 | |||
450 | if ca_len < 4: |
||
451 | current_row_number = current_row_number - ca_len + 4 |
||
452 | |||
453 | else: |
||
454 | for i in range(21, 29 + 1): |
||
455 | current_row_number = 30 |
||
456 | ws.row_dimensions[i].height = 0.1 |
||
457 | |||
458 | ############################################### |
||
459 | |||
460 | current_row_number += 1 |
||
461 | |||
462 | has_detail_data_flag = True |
||
463 | |||
464 | table_start_draw_flag = current_row_number + 1 |
||
465 | |||
466 | if "timestamps" not in reporting_period_data.keys() or \ |
||
467 | reporting_period_data['timestamps'] is None or \ |
||
468 | len(reporting_period_data['timestamps']) == 0: |
||
469 | has_detail_data_flag = False |
||
470 | |||
471 | View Code Duplication | if has_detail_data_flag: |
|
472 | reporting_period_data = report['reporting_period'] |
||
473 | times = reporting_period_data['timestamps'] |
||
474 | ca_len = len(report['reporting_period']['names']) |
||
475 | |||
476 | ws['B' + str(current_row_number)].font = title_font |
||
477 | ws['B' + str(current_row_number)] = name+' 详细数据' |
||
478 | |||
479 | table_start_row_number = (current_row_number + 1) + ca_len * 6 |
||
480 | current_row_number = table_start_row_number |
||
481 | |||
482 | time = times[0] |
||
483 | has_data = False |
||
484 | |||
485 | if len(time) > 0: |
||
486 | has_data = True |
||
487 | |||
488 | if has_data: |
||
489 | |||
490 | ws.row_dimensions[current_row_number].height = 60 |
||
491 | ws['B' + str(current_row_number)].fill = table_fill |
||
492 | ws['B' + str(current_row_number)].border = f_border |
||
493 | ws['B' + str(current_row_number)].font = title_font |
||
494 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
495 | ws['B' + str(current_row_number)] = '日期时间' |
||
496 | |||
497 | col = 'B' |
||
498 | |||
499 | for i in range(0, ca_len): |
||
500 | col = chr(ord('C') + i) |
||
501 | |||
502 | ws[col + str(current_row_number)].fill = table_fill |
||
503 | ws[col + str(current_row_number)].font = title_font |
||
504 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
505 | ws[col + str(current_row_number)] = reporting_period_data['names'][i] + \ |
||
506 | " (" + reporting_period_data['units'][i] + ")" |
||
507 | ws[col + str(current_row_number)].border = f_border |
||
508 | |||
509 | end_col = chr(ord(col) + 1) |
||
510 | |||
511 | ws[end_col + str(current_row_number)].fill = table_fill |
||
512 | ws[end_col + str(current_row_number)].font = title_font |
||
513 | ws[end_col + str(current_row_number)].alignment = c_c_alignment |
||
514 | ws[end_col + str(current_row_number)] = "总计 (" + reporting_period_data['total_unit'] + ")" |
||
515 | ws[end_col + str(current_row_number)].border = f_border |
||
516 | |||
517 | current_row_number += 1 |
||
518 | |||
519 | for i in range(0, len(time)): |
||
520 | ws['B' + str(current_row_number)].font = title_font |
||
521 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
522 | ws['B' + str(current_row_number)] = time[i] |
||
523 | ws['B' + str(current_row_number)].border = f_border |
||
524 | |||
525 | col = 'B' |
||
526 | |||
527 | every_day_total = 0 |
||
528 | |||
529 | for j in range(0, ca_len): |
||
530 | col = chr(ord('C') + j) |
||
531 | |||
532 | ws[col + str(current_row_number)].font = title_font |
||
533 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
534 | value = round(reporting_period_data['values'][j][i], 2) |
||
535 | every_day_total += value |
||
536 | ws[col + str(current_row_number)] = value |
||
537 | ws[col + str(current_row_number)].border = f_border |
||
538 | |||
539 | end_col = chr(ord(col) + 1) |
||
540 | ws[end_col + str(current_row_number)].font = title_font |
||
541 | ws[end_col + str(current_row_number)].alignment = c_c_alignment |
||
542 | ws[end_col + str(current_row_number)] = round(every_day_total, 2) |
||
543 | ws[end_col + str(current_row_number)].border = f_border |
||
544 | |||
545 | current_row_number += 1 |
||
546 | |||
547 | table_end_row_number = current_row_number - 1 |
||
548 | |||
549 | ws['B' + str(current_row_number)].font = title_font |
||
550 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
551 | ws['B' + str(current_row_number)] = '小计' |
||
552 | ws['B' + str(current_row_number)].border = f_border |
||
553 | |||
554 | col = 'B' |
||
555 | |||
556 | for i in range(0, ca_len): |
||
557 | col = chr(ord('C') + i) |
||
558 | ws[col + str(current_row_number)].font = title_font |
||
559 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
560 | ws[col + str(current_row_number)] = round(reporting_period_data['subtotals'][i], 2) |
||
561 | ws[col + str(current_row_number)].border = f_border |
||
562 | |||
563 | # line |
||
564 | line = LineChart() |
||
565 | line.title = '报告期成本 - ' + ws.cell(column=3+i, row=table_start_row_number).value |
||
566 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
567 | line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, max_row=table_end_row_number) |
||
568 | line.add_data(line_data, titles_from_data=True) |
||
569 | line.set_categories(labels) |
||
570 | line_data = line.series[0] |
||
571 | line_data.marker.symbol = "circle" |
||
572 | line_data.smooth = True |
||
573 | line.x_axis.crosses = 'min' |
||
574 | line.height = 8.25 |
||
575 | line.width = 24 |
||
576 | line.dLbls = DataLabelList() |
||
577 | line.dLbls.dLblPos = 't' |
||
578 | line.dLbls.showVal = True |
||
579 | line.dLbls.showPercent = False |
||
580 | chart_col = 'B' |
||
581 | chart_cell = chart_col + str(table_start_draw_flag + 6 * i) |
||
582 | ws.add_chart(line, chart_cell) |
||
583 | |||
584 | end_col = chr(ord(col) + 1) |
||
585 | ws[end_col + str(current_row_number)].font = title_font |
||
586 | ws[end_col + str(current_row_number)].alignment = c_c_alignment |
||
587 | ws[end_col + str(current_row_number)] = round(reporting_period_data['total'], 2) |
||
588 | ws[end_col + str(current_row_number)].border = f_border |
||
589 | |||
590 | current_row_number += 1 |
||
591 | |||
592 | else: |
||
593 | for i in range(30, 69 + 1): |
||
594 | current_row_number = 70 |
||
595 | ws.row_dimensions[i].height = 0.1 |
||
596 | |||
597 | filename = str(uuid.uuid4()) + '.xlsx' |
||
598 | wb.save(filename) |
||
599 | |||
600 | return filename |
||
601 |