| @@ 61-345 (lines=285) @@ | ||
| 58 | return base64_message |
|
| 59 | ||
| 60 | ||
| 61 | def generate_excel(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type): |
|
| 62 | ||
| 63 | wb = Workbook() |
|
| 64 | ||
| 65 | # todo |
|
| 66 | ws = wb.active |
|
| 67 | ||
| 68 | # Row height |
|
| 69 | ws.row_dimensions[1].height = 118 |
|
| 70 | for i in range(2, 11 + 1): |
|
| 71 | ws.row_dimensions[i].height = 30 |
|
| 72 | ||
| 73 | for i in range(12, 43 + 1): |
|
| 74 | ws.row_dimensions[i].height = 30 |
|
| 75 | ||
| 76 | # Col width |
|
| 77 | ws.column_dimensions['A'].width = 1.5 |
|
| 78 | ||
| 79 | for i in range(ord('B'), ord('I')): |
|
| 80 | ws.column_dimensions[chr(i)].width = 15.0 |
|
| 81 | ||
| 82 | # Font |
|
| 83 | name_font = Font(name='Constantia', size=15, bold=True) |
|
| 84 | title_font = Font(name='宋体', size=15, bold=True) |
|
| 85 | data_font = Font(name='Franklin Gothic Book', size=11) |
|
| 86 | ||
| 87 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
|
| 88 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
|
| 89 | right=Side(border_style='medium', color='00000000'), |
|
| 90 | bottom=Side(border_style='medium', color='00000000'), |
|
| 91 | top=Side(border_style='medium', color='00000000') |
|
| 92 | ) |
|
| 93 | b_border = Border( |
|
| 94 | bottom=Side(border_style='medium', color='00000000'), |
|
| 95 | ) |
|
| 96 | ||
| 97 | b_c_alignment = Alignment(vertical='bottom', |
|
| 98 | horizontal='center', |
|
| 99 | text_rotation=0, |
|
| 100 | wrap_text=False, |
|
| 101 | shrink_to_fit=False, |
|
| 102 | indent=0) |
|
| 103 | c_c_alignment = Alignment(vertical='center', |
|
| 104 | horizontal='center', |
|
| 105 | text_rotation=0, |
|
| 106 | wrap_text=False, |
|
| 107 | shrink_to_fit=False, |
|
| 108 | indent=0) |
|
| 109 | b_r_alignment = Alignment(vertical='bottom', |
|
| 110 | horizontal='right', |
|
| 111 | text_rotation=0, |
|
| 112 | wrap_text=False, |
|
| 113 | shrink_to_fit=False, |
|
| 114 | indent=0) |
|
| 115 | c_r_alignment = Alignment(vertical='bottom', |
|
| 116 | horizontal='center', |
|
| 117 | text_rotation=0, |
|
| 118 | wrap_text=False, |
|
| 119 | shrink_to_fit=False, |
|
| 120 | indent=0) |
|
| 121 | ||
| 122 | # Img |
|
| 123 | img = Image("excelexporters/myems.png") |
|
| 124 | ws.add_image(img, 'B1') |
|
| 125 | ||
| 126 | # Title |
|
| 127 | ws['B3'].font = name_font |
|
| 128 | ws['B3'].alignment = b_r_alignment |
|
| 129 | ws['B3'] = 'Name:' |
|
| 130 | ws['C3'].border = b_border |
|
| 131 | ws['C3'].alignment = b_c_alignment |
|
| 132 | ws['C3'].font = name_font |
|
| 133 | ws['C3'] = name |
|
| 134 | ||
| 135 | ws['D3'].font = name_font |
|
| 136 | ws['D3'].alignment = b_r_alignment |
|
| 137 | ws['D3'] = 'Period:' |
|
| 138 | ws['E3'].border = b_border |
|
| 139 | ws['E3'].alignment = b_c_alignment |
|
| 140 | ws['E3'].font = name_font |
|
| 141 | ws['E3'] = period_type |
|
| 142 | ||
| 143 | ws['F3'].font = name_font |
|
| 144 | ws['F3'].alignment = b_r_alignment |
|
| 145 | ws['F3'] = 'Date:' |
|
| 146 | ws.merge_cells("G3:H3") |
|
| 147 | ws['G3'].border = b_border |
|
| 148 | ws['G3'].alignment = b_c_alignment |
|
| 149 | ws['G3'].font = name_font |
|
| 150 | ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
|
| 151 | ||
| 152 | if "reporting_period" not in report.keys() or \ |
|
| 153 | "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
|
| 154 | filename = str(uuid.uuid4()) + '.xlsx' |
|
| 155 | wb.save(filename) |
|
| 156 | ||
| 157 | return filename |
|
| 158 | ||
| 159 | ############################### |
|
| 160 | ||
| 161 | has_cost_data_flag = True |
|
| 162 | ||
| 163 | if "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
|
| 164 | has_cost_data_flag = False |
|
| 165 | ||
| 166 | if has_cost_data_flag: |
|
| 167 | ws['B6'].font = title_font |
|
| 168 | ws['B6'] = name + '报告期成本' |
|
| 169 | ||
| 170 | reporting_period_data = report['reporting_period'] |
|
| 171 | category = report['virtual_meter']['energy_category_name'] |
|
| 172 | ca_len = len(category) |
|
| 173 | ||
| 174 | ws['B7'].fill = table_fill |
|
| 175 | ||
| 176 | ws['B8'].font = title_font |
|
| 177 | ws['B8'].alignment = c_c_alignment |
|
| 178 | ws['B8'] = '成本' |
|
| 179 | ws['B8'].border = f_border |
|
| 180 | ||
| 181 | ws['B9'].font = title_font |
|
| 182 | ws['B9'].alignment = c_c_alignment |
|
| 183 | ws['B9'] = '环比' |
|
| 184 | ws['B9'].border = f_border |
|
| 185 | ||
| 186 | col = 'B' |
|
| 187 | ||
| 188 | for i in range(0, ca_len): |
|
| 189 | col = chr(ord('C') + i) |
|
| 190 | ||
| 191 | ws[col + '7'].fill = table_fill |
|
| 192 | ws[col + '7'].font = name_font |
|
| 193 | ws[col + '7'].alignment = c_c_alignment |
|
| 194 | ws[col + '7'] = report['virtual_meter']['energy_category_name'] + \ |
|
| 195 | " (" + report['virtual_meter']['unit_of_measure'] + ")" |
|
| 196 | ws[col + '7'].border = f_border |
|
| 197 | ||
| 198 | ws[col + '8'].font = name_font |
|
| 199 | ws[col + '8'].alignment = c_c_alignment |
|
| 200 | ws[col + '8'] = round(reporting_period_data['total_in_category'], 0) |
|
| 201 | ws[col + '8'].border = f_border |
|
| 202 | ||
| 203 | ws[col + '9'].font = name_font |
|
| 204 | ws[col + '9'].alignment = c_c_alignment |
|
| 205 | ws[col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
|
| 206 | if reporting_period_data['increment_rate'] is not None else "-" |
|
| 207 | ws[col + '9'].border = f_border |
|
| 208 | ||
| 209 | # TCE TCO2E |
|
| 210 | end_col = col |
|
| 211 | # TCE |
|
| 212 | tce_col = chr(ord(end_col) + 1) |
|
| 213 | ws[tce_col + '7'].fill = table_fill |
|
| 214 | ws[tce_col + '7'].font = name_font |
|
| 215 | ws[tce_col + '7'].alignment = c_c_alignment |
|
| 216 | ws[tce_col + '7'] = "TCE" |
|
| 217 | ws[tce_col + '7'].border = f_border |
|
| 218 | ||
| 219 | ws[tce_col + '8'].font = name_font |
|
| 220 | ws[tce_col + '8'].alignment = c_c_alignment |
|
| 221 | ws[tce_col + '8'] = round(reporting_period_data['total_in_kgce'], 0) |
|
| 222 | ws[tce_col + '8'].border = f_border |
|
| 223 | ||
| 224 | ws[tce_col + '9'].font = name_font |
|
| 225 | ws[tce_col + '9'].alignment = c_c_alignment |
|
| 226 | ws[tce_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
|
| 227 | if reporting_period_data['increment_rate'] is not None else "-" |
|
| 228 | ws[tce_col + '9'].border = f_border |
|
| 229 | ||
| 230 | # TCO2E |
|
| 231 | tco2e_col = chr(ord(end_col) + 2) |
|
| 232 | ws[tco2e_col + '7'].fill = table_fill |
|
| 233 | ws[tco2e_col + '7'].font = name_font |
|
| 234 | ws[tco2e_col + '7'].alignment = c_c_alignment |
|
| 235 | ws[tco2e_col + '7'] = "TCO2E" |
|
| 236 | ws[tco2e_col + '7'].border = f_border |
|
| 237 | ||
| 238 | ws[tco2e_col + '8'].font = name_font |
|
| 239 | ws[tco2e_col + '8'].alignment = c_c_alignment |
|
| 240 | ws[tco2e_col + '8'] = round(reporting_period_data['total_in_kgco2e'], 0) |
|
| 241 | ws[tco2e_col + '8'].border = f_border |
|
| 242 | ||
| 243 | ws[tco2e_col + '9'].font = name_font |
|
| 244 | ws[tco2e_col + '9'].alignment = c_c_alignment |
|
| 245 | ws[tco2e_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
|
| 246 | if reporting_period_data['increment_rate'] is not None else "-" |
|
| 247 | ws[tco2e_col + '9'].border = f_border |
|
| 248 | ||
| 249 | else: |
|
| 250 | for i in range(6, 9 + 1): |
|
| 251 | ws.rows_dimensions[i].height = 0.1 |
|
| 252 | ||
| 253 | ###################################### |
|
| 254 | ||
| 255 | has_cost_detail_flag = True |
|
| 256 | reporting_period_data = report['reporting_period'] |
|
| 257 | category = report['virtual_meter']['energy_category_name'] |
|
| 258 | ca_len = len(category) |
|
| 259 | times = reporting_period_data['timestamps'] |
|
| 260 | ||
| 261 | if "values" not in reporting_period_data.keys() or len(reporting_period_data['values']) == 0: |
|
| 262 | has_cost_detail_flag = False |
|
| 263 | ||
| 264 | if has_cost_detail_flag: |
|
| 265 | ws['B11'].font = title_font |
|
| 266 | ws['B11'] = name + '详细数据' |
|
| 267 | ||
| 268 | ws['B18'].fill = table_fill |
|
| 269 | ws['B18'].font = title_font |
|
| 270 | ws['B18'].border = f_border |
|
| 271 | ws['B18'].alignment = c_c_alignment |
|
| 272 | ws['B18'] = '日期时间' |
|
| 273 | time = times |
|
| 274 | has_data = False |
|
| 275 | max_row = 0 |
|
| 276 | if len(time) > 0: |
|
| 277 | has_data = True |
|
| 278 | max_row = 18 + len(time) |
|
| 279 | ||
| 280 | if has_data: |
|
| 281 | ||
| 282 | end_data_flag = 19 |
|
| 283 | ||
| 284 | for i in range(0, len(time)): |
|
| 285 | col = 'B' |
|
| 286 | end_data_flag = 19 + i |
|
| 287 | row = str(end_data_flag) |
|
| 288 | ||
| 289 | ws[col + row].font = title_font |
|
| 290 | ws[col + row].alignment = c_c_alignment |
|
| 291 | ws[col + row] = time[i] |
|
| 292 | ws[col + row].border = f_border |
|
| 293 | ||
| 294 | ws['B' + str(end_data_flag + 1)].font = title_font |
|
| 295 | ws['B' + str(end_data_flag + 1)].alignment = c_c_alignment |
|
| 296 | ws['B' + str(end_data_flag + 1)] = '总计' |
|
| 297 | ws['B' + str(end_data_flag + 1)].border = f_border |
|
| 298 | ||
| 299 | bar = BarChart() |
|
| 300 | ||
| 301 | for i in range(0, ca_len): |
|
| 302 | ||
| 303 | col = chr(ord('C') + i) |
|
| 304 | ||
| 305 | ws[col + '18'].fill = table_fill |
|
| 306 | ws[col + '18'].font = title_font |
|
| 307 | ws[col + '18'].alignment = c_c_alignment |
|
| 308 | ws[col + '18'] = report['virtual_meter']['energy_category_name'] + \ |
|
| 309 | " (" + report['virtual_meter']['unit_of_measure'] + ")" |
|
| 310 | ws[col + '18'].border = f_border |
|
| 311 | ||
| 312 | time = times |
|
| 313 | time_len = len(time) |
|
| 314 | ||
| 315 | for j in range(0, time_len): |
|
| 316 | row = str(19 + j) |
|
| 317 | ||
| 318 | ws[col + row].font = title_font |
|
| 319 | ws[col + row].alignment = c_c_alignment |
|
| 320 | ws[col + row] = round(reporting_period_data['values'][j], 0) |
|
| 321 | ws[col + row].border = f_border |
|
| 322 | ||
| 323 | ws[col + str(end_data_flag + 1)].font = title_font |
|
| 324 | ws[col + str(end_data_flag + 1)].alignment = c_c_alignment |
|
| 325 | ws[col + str(end_data_flag + 1)] = round(reporting_period_data['total_in_category'], 0) |
|
| 326 | ws[col + str(end_data_flag + 1)].border = f_border |
|
| 327 | ||
| 328 | bar_data = Reference(ws, min_col=3 + i, min_row=18, max_row=max_row) |
|
| 329 | bar.series.append(Series(bar_data, title_from_data=True)) |
|
| 330 | ||
| 331 | labels = Reference(ws, min_col=2, min_row=19, max_row=max_row) |
|
| 332 | bar.set_categories(labels) |
|
| 333 | bar.dLbls = DataLabelList() |
|
| 334 | bar.dLbls.showVal = True |
|
| 335 | bar.height = 5.25 |
|
| 336 | bar.width = len(time) |
|
| 337 | ws.add_chart(bar, "B12") |
|
| 338 | else: |
|
| 339 | for i in range(11, 43 + 1): |
|
| 340 | ws.row_dimensions[i].height = 0.0 |
|
| 341 | ||
| 342 | filename = str(uuid.uuid4()) + '.xlsx' |
|
| 343 | wb.save(filename) |
|
| 344 | ||
| 345 | return filename |
|
| 346 | ||
| @@ 61-345 (lines=285) @@ | ||
| 58 | return base64_message |
|
| 59 | ||
| 60 | ||
| 61 | def generate_excel(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type): |
|
| 62 | ||
| 63 | wb = Workbook() |
|
| 64 | ||
| 65 | # todo |
|
| 66 | ws = wb.active |
|
| 67 | ||
| 68 | # Row height |
|
| 69 | ws.row_dimensions[1].height = 118 |
|
| 70 | for i in range(2, 11 + 1): |
|
| 71 | ws.row_dimensions[i].height = 30 |
|
| 72 | ||
| 73 | for i in range(12, 43 + 1): |
|
| 74 | ws.row_dimensions[i].height = 30 |
|
| 75 | ||
| 76 | # Col width |
|
| 77 | ws.column_dimensions['A'].width = 1.5 |
|
| 78 | ||
| 79 | for i in range(ord('B'), ord('I')): |
|
| 80 | ws.column_dimensions[chr(i)].width = 15.0 |
|
| 81 | ||
| 82 | # Font |
|
| 83 | name_font = Font(name='Constantia', size=15, bold=True) |
|
| 84 | title_font = Font(name='宋体', size=15, bold=True) |
|
| 85 | data_font = Font(name='Franklin Gothic Book', size=11) |
|
| 86 | ||
| 87 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
|
| 88 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
|
| 89 | right=Side(border_style='medium', color='00000000'), |
|
| 90 | bottom=Side(border_style='medium', color='00000000'), |
|
| 91 | top=Side(border_style='medium', color='00000000') |
|
| 92 | ) |
|
| 93 | b_border = Border( |
|
| 94 | bottom=Side(border_style='medium', color='00000000'), |
|
| 95 | ) |
|
| 96 | ||
| 97 | b_c_alignment = Alignment(vertical='bottom', |
|
| 98 | horizontal='center', |
|
| 99 | text_rotation=0, |
|
| 100 | wrap_text=False, |
|
| 101 | shrink_to_fit=False, |
|
| 102 | indent=0) |
|
| 103 | c_c_alignment = Alignment(vertical='center', |
|
| 104 | horizontal='center', |
|
| 105 | text_rotation=0, |
|
| 106 | wrap_text=False, |
|
| 107 | shrink_to_fit=False, |
|
| 108 | indent=0) |
|
| 109 | b_r_alignment = Alignment(vertical='bottom', |
|
| 110 | horizontal='right', |
|
| 111 | text_rotation=0, |
|
| 112 | wrap_text=False, |
|
| 113 | shrink_to_fit=False, |
|
| 114 | indent=0) |
|
| 115 | c_r_alignment = Alignment(vertical='bottom', |
|
| 116 | horizontal='center', |
|
| 117 | text_rotation=0, |
|
| 118 | wrap_text=False, |
|
| 119 | shrink_to_fit=False, |
|
| 120 | indent=0) |
|
| 121 | ||
| 122 | # Img |
|
| 123 | img = Image("excelexporters/myems.png") |
|
| 124 | ws.add_image(img, 'B1') |
|
| 125 | ||
| 126 | # Title |
|
| 127 | ws['B3'].font = name_font |
|
| 128 | ws['B3'].alignment = b_r_alignment |
|
| 129 | ws['B3'] = 'Name:' |
|
| 130 | ws['C3'].border = b_border |
|
| 131 | ws['C3'].alignment = b_c_alignment |
|
| 132 | ws['C3'].font = name_font |
|
| 133 | ws['C3'] = name |
|
| 134 | ||
| 135 | ws['D3'].font = name_font |
|
| 136 | ws['D3'].alignment = b_r_alignment |
|
| 137 | ws['D3'] = 'Period:' |
|
| 138 | ws['E3'].border = b_border |
|
| 139 | ws['E3'].alignment = b_c_alignment |
|
| 140 | ws['E3'].font = name_font |
|
| 141 | ws['E3'] = period_type |
|
| 142 | ||
| 143 | ws['F3'].font = name_font |
|
| 144 | ws['F3'].alignment = b_r_alignment |
|
| 145 | ws['F3'] = 'Date:' |
|
| 146 | ws.merge_cells("G3:H3") |
|
| 147 | ws['G3'].border = b_border |
|
| 148 | ws['G3'].alignment = b_c_alignment |
|
| 149 | ws['G3'].font = name_font |
|
| 150 | ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
|
| 151 | ||
| 152 | if "reporting_period" not in report.keys() or \ |
|
| 153 | "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
|
| 154 | filename = str(uuid.uuid4()) + '.xlsx' |
|
| 155 | wb.save(filename) |
|
| 156 | ||
| 157 | return filename |
|
| 158 | ||
| 159 | ############################### |
|
| 160 | ||
| 161 | has_cost_data_flag = True |
|
| 162 | ||
| 163 | if "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
|
| 164 | has_cost_data_flag = False |
|
| 165 | ||
| 166 | if has_cost_data_flag: |
|
| 167 | ws['B6'].font = title_font |
|
| 168 | ws['B6'] = name + '报告期成本' |
|
| 169 | ||
| 170 | reporting_period_data = report['reporting_period'] |
|
| 171 | category = report['meter']['energy_category_name'] |
|
| 172 | ca_len = len(category) |
|
| 173 | ||
| 174 | ws['B7'].fill = table_fill |
|
| 175 | ||
| 176 | ws['B8'].font = title_font |
|
| 177 | ws['B8'].alignment = c_c_alignment |
|
| 178 | ws['B8'] = '成本' |
|
| 179 | ws['B8'].border = f_border |
|
| 180 | ||
| 181 | ws['B9'].font = title_font |
|
| 182 | ws['B9'].alignment = c_c_alignment |
|
| 183 | ws['B9'] = '环比' |
|
| 184 | ws['B9'].border = f_border |
|
| 185 | ||
| 186 | col = 'B' |
|
| 187 | ||
| 188 | for i in range(0, ca_len): |
|
| 189 | col = chr(ord('C') + i) |
|
| 190 | ||
| 191 | ws[col + '7'].fill = table_fill |
|
| 192 | ws[col + '7'].font = name_font |
|
| 193 | ws[col + '7'].alignment = c_c_alignment |
|
| 194 | ws[col + '7'] = report['meter']['energy_category_name'] + \ |
|
| 195 | " (" + report['meter']['unit_of_measure'] + ")" |
|
| 196 | ws[col + '7'].border = f_border |
|
| 197 | ||
| 198 | ws[col + '8'].font = name_font |
|
| 199 | ws[col + '8'].alignment = c_c_alignment |
|
| 200 | ws[col + '8'] = round(reporting_period_data['total_in_category'], 0) |
|
| 201 | ws[col + '8'].border = f_border |
|
| 202 | ||
| 203 | ws[col + '9'].font = name_font |
|
| 204 | ws[col + '9'].alignment = c_c_alignment |
|
| 205 | ws[col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
|
| 206 | if reporting_period_data['increment_rate'] is not None else "-" |
|
| 207 | ws[col + '9'].border = f_border |
|
| 208 | ||
| 209 | # TCE TCO2E |
|
| 210 | end_col = col |
|
| 211 | # TCE |
|
| 212 | tce_col = chr(ord(end_col) + 1) |
|
| 213 | ws[tce_col + '7'].fill = table_fill |
|
| 214 | ws[tce_col + '7'].font = name_font |
|
| 215 | ws[tce_col + '7'].alignment = c_c_alignment |
|
| 216 | ws[tce_col + '7'] = "TCE" |
|
| 217 | ws[tce_col + '7'].border = f_border |
|
| 218 | ||
| 219 | ws[tce_col + '8'].font = name_font |
|
| 220 | ws[tce_col + '8'].alignment = c_c_alignment |
|
| 221 | ws[tce_col + '8'] = round(reporting_period_data['total_in_kgce'], 0) |
|
| 222 | ws[tce_col + '8'].border = f_border |
|
| 223 | ||
| 224 | ws[tce_col + '9'].font = name_font |
|
| 225 | ws[tce_col + '9'].alignment = c_c_alignment |
|
| 226 | ws[tce_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
|
| 227 | if reporting_period_data['increment_rate'] is not None else "-" |
|
| 228 | ws[tce_col + '9'].border = f_border |
|
| 229 | ||
| 230 | # TCO2E |
|
| 231 | tco2e_col = chr(ord(end_col) + 2) |
|
| 232 | ws[tco2e_col + '7'].fill = table_fill |
|
| 233 | ws[tco2e_col + '7'].font = name_font |
|
| 234 | ws[tco2e_col + '7'].alignment = c_c_alignment |
|
| 235 | ws[tco2e_col + '7'] = "TCO2E" |
|
| 236 | ws[tco2e_col + '7'].border = f_border |
|
| 237 | ||
| 238 | ws[tco2e_col + '8'].font = name_font |
|
| 239 | ws[tco2e_col + '8'].alignment = c_c_alignment |
|
| 240 | ws[tco2e_col + '8'] = round(reporting_period_data['total_in_kgco2e'], 0) |
|
| 241 | ws[tco2e_col + '8'].border = f_border |
|
| 242 | ||
| 243 | ws[tco2e_col + '9'].font = name_font |
|
| 244 | ws[tco2e_col + '9'].alignment = c_c_alignment |
|
| 245 | ws[tco2e_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
|
| 246 | if reporting_period_data['increment_rate'] is not None else "-" |
|
| 247 | ws[tco2e_col + '9'].border = f_border |
|
| 248 | ||
| 249 | else: |
|
| 250 | for i in range(6, 9 + 1): |
|
| 251 | ws.rows_dimensions[i].height = 0.1 |
|
| 252 | ||
| 253 | ###################################### |
|
| 254 | ||
| 255 | has_cost_datail_flag = True |
|
| 256 | reporting_period_data = report['reporting_period'] |
|
| 257 | category = report['meter']['energy_category_name'] |
|
| 258 | ca_len = len(category) |
|
| 259 | times = reporting_period_data['timestamps'] |
|
| 260 | ||
| 261 | if "values" not in reporting_period_data.keys() or len(reporting_period_data['values']) == 0: |
|
| 262 | has_cost_datail_flag = False |
|
| 263 | ||
| 264 | if has_cost_datail_flag: |
|
| 265 | ws['B11'].font = title_font |
|
| 266 | ws['B11'] = name + '详细数据' |
|
| 267 | ||
| 268 | ws['B18'].fill = table_fill |
|
| 269 | ws['B18'].font = title_font |
|
| 270 | ws['B18'].border = f_border |
|
| 271 | ws['B18'].alignment = c_c_alignment |
|
| 272 | ws['B18'] = '日期时间' |
|
| 273 | time = times |
|
| 274 | has_data = False |
|
| 275 | max_row = 0 |
|
| 276 | if len(time) > 0: |
|
| 277 | has_data = True |
|
| 278 | max_row = 18 + len(time) |
|
| 279 | ||
| 280 | if has_data: |
|
| 281 | ||
| 282 | end_data_flag = 19 |
|
| 283 | ||
| 284 | for i in range(0, len(time)): |
|
| 285 | col = 'B' |
|
| 286 | end_data_flag = 19 + i |
|
| 287 | row = str(end_data_flag) |
|
| 288 | ||
| 289 | ws[col + row].font = title_font |
|
| 290 | ws[col + row].alignment = c_c_alignment |
|
| 291 | ws[col + row] = time[i] |
|
| 292 | ws[col + row].border = f_border |
|
| 293 | ||
| 294 | ws['B' + str(end_data_flag + 1)].font = title_font |
|
| 295 | ws['B' + str(end_data_flag + 1)].alignment = c_c_alignment |
|
| 296 | ws['B' + str(end_data_flag + 1)] = '总计' |
|
| 297 | ws['B' + str(end_data_flag + 1)].border = f_border |
|
| 298 | ||
| 299 | bar = BarChart() |
|
| 300 | ||
| 301 | for i in range(0, ca_len): |
|
| 302 | ||
| 303 | col = chr(ord('C') + i) |
|
| 304 | ||
| 305 | ws[col + '18'].fill = table_fill |
|
| 306 | ws[col + '18'].font = title_font |
|
| 307 | ws[col + '18'].alignment = c_c_alignment |
|
| 308 | ws[col + '18'] = report['meter']['energy_category_name'] + \ |
|
| 309 | " (" + report['meter']['unit_of_measure'] + ")" |
|
| 310 | ws[col + '18'].border = f_border |
|
| 311 | ||
| 312 | time = times |
|
| 313 | time_len = len(time) |
|
| 314 | ||
| 315 | for j in range(0, time_len): |
|
| 316 | row = str(19 + j) |
|
| 317 | ||
| 318 | ws[col + row].font = title_font |
|
| 319 | ws[col + row].alignment = c_c_alignment |
|
| 320 | ws[col + row] = round(reporting_period_data['values'][j], 0) |
|
| 321 | ws[col + row].border = f_border |
|
| 322 | ||
| 323 | ws[col + str(end_data_flag + 1)].font = title_font |
|
| 324 | ws[col + str(end_data_flag + 1)].alignment = c_c_alignment |
|
| 325 | ws[col + str(end_data_flag + 1)] = round(reporting_period_data['total_in_category'], 0) |
|
| 326 | ws[col + str(end_data_flag + 1)].border = f_border |
|
| 327 | ||
| 328 | bar_data = Reference(ws, min_col=3 + i, min_row=18, max_row=max_row) |
|
| 329 | bar.series.append(Series(bar_data, title_from_data=True)) |
|
| 330 | ||
| 331 | labels = Reference(ws, min_col=2, min_row=19, max_row=max_row) |
|
| 332 | bar.set_categories(labels) |
|
| 333 | bar.dLbls = DataLabelList() |
|
| 334 | bar.dLbls.showVal = True |
|
| 335 | bar.height = 5.25 |
|
| 336 | bar.width = len(time) |
|
| 337 | ws.add_chart(bar, "B12") |
|
| 338 | else: |
|
| 339 | for i in range(11, 43 + 1): |
|
| 340 | ws.row_dimensions[i].height = 0.0 |
|
| 341 | ||
| 342 | filename = str(uuid.uuid4()) + '.xlsx' |
|
| 343 | wb.save(filename) |
|
| 344 | ||
| 345 | return filename |
|
| 346 | ||
| @@ 61-344 (lines=284) @@ | ||
| 58 | return base64_message |
|
| 59 | ||
| 60 | ||
| 61 | def generate_excel(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type): |
|
| 62 | wb = Workbook() |
|
| 63 | ||
| 64 | # todo |
|
| 65 | ws = wb.active |
|
| 66 | ||
| 67 | # Row height |
|
| 68 | ws.row_dimensions[1].height = 118 |
|
| 69 | for i in range(2, 11 + 1): |
|
| 70 | ws.row_dimensions[i].height = 30 |
|
| 71 | ||
| 72 | for i in range(12, 43 + 1): |
|
| 73 | ws.row_dimensions[i].height = 30 |
|
| 74 | ||
| 75 | # Col width |
|
| 76 | ws.column_dimensions['A'].width = 1.5 |
|
| 77 | ||
| 78 | for i in range(ord('B'), ord('I')): |
|
| 79 | ws.column_dimensions[chr(i)].width = 15.0 |
|
| 80 | ||
| 81 | # Font |
|
| 82 | name_font = Font(name='Constantia', size=15, bold=True) |
|
| 83 | title_font = Font(name='宋体', size=15, bold=True) |
|
| 84 | data_font = Font(name='Franklin Gothic Book', size=11) |
|
| 85 | ||
| 86 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
|
| 87 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
|
| 88 | right=Side(border_style='medium', color='00000000'), |
|
| 89 | bottom=Side(border_style='medium', color='00000000'), |
|
| 90 | top=Side(border_style='medium', color='00000000') |
|
| 91 | ) |
|
| 92 | b_border = Border( |
|
| 93 | bottom=Side(border_style='medium', color='00000000'), |
|
| 94 | ) |
|
| 95 | ||
| 96 | b_c_alignment = Alignment(vertical='bottom', |
|
| 97 | horizontal='center', |
|
| 98 | text_rotation=0, |
|
| 99 | wrap_text=False, |
|
| 100 | shrink_to_fit=False, |
|
| 101 | indent=0) |
|
| 102 | c_c_alignment = Alignment(vertical='center', |
|
| 103 | horizontal='center', |
|
| 104 | text_rotation=0, |
|
| 105 | wrap_text=False, |
|
| 106 | shrink_to_fit=False, |
|
| 107 | indent=0) |
|
| 108 | b_r_alignment = Alignment(vertical='bottom', |
|
| 109 | horizontal='right', |
|
| 110 | text_rotation=0, |
|
| 111 | wrap_text=False, |
|
| 112 | shrink_to_fit=False, |
|
| 113 | indent=0) |
|
| 114 | c_r_alignment = Alignment(vertical='bottom', |
|
| 115 | horizontal='center', |
|
| 116 | text_rotation=0, |
|
| 117 | wrap_text=False, |
|
| 118 | shrink_to_fit=False, |
|
| 119 | indent=0) |
|
| 120 | ||
| 121 | # Img |
|
| 122 | img = Image("excelexporters/myems.png") |
|
| 123 | ws.add_image(img, 'B1') |
|
| 124 | ||
| 125 | # Title |
|
| 126 | ws['B3'].font = name_font |
|
| 127 | ws['B3'].alignment = b_r_alignment |
|
| 128 | ws['B3'] = 'Name:' |
|
| 129 | ws['C3'].border = b_border |
|
| 130 | ws['C3'].alignment = b_c_alignment |
|
| 131 | ws['C3'].font = name_font |
|
| 132 | ws['C3'] = name |
|
| 133 | ||
| 134 | ws['D3'].font = name_font |
|
| 135 | ws['D3'].alignment = b_r_alignment |
|
| 136 | ws['D3'] = 'Period:' |
|
| 137 | ws['E3'].border = b_border |
|
| 138 | ws['E3'].alignment = b_c_alignment |
|
| 139 | ws['E3'].font = name_font |
|
| 140 | ws['E3'] = period_type |
|
| 141 | ||
| 142 | ws['F3'].font = name_font |
|
| 143 | ws['F3'].alignment = b_r_alignment |
|
| 144 | ws['F3'] = 'Date:' |
|
| 145 | ws.merge_cells("G3:H3") |
|
| 146 | ws['G3'].border = b_border |
|
| 147 | ws['G3'].alignment = b_c_alignment |
|
| 148 | ws['G3'].font = name_font |
|
| 149 | ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
|
| 150 | ||
| 151 | if "reporting_period" not in report.keys() or \ |
|
| 152 | "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
|
| 153 | filename = str(uuid.uuid4()) + '.xlsx' |
|
| 154 | wb.save(filename) |
|
| 155 | ||
| 156 | return filename |
|
| 157 | ||
| 158 | ############################### |
|
| 159 | ||
| 160 | has_cost_data_flag = True |
|
| 161 | ||
| 162 | if "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
|
| 163 | has_cost_data_flag = False |
|
| 164 | ||
| 165 | if has_cost_data_flag: |
|
| 166 | ws['B6'].font = title_font |
|
| 167 | ws['B6'] = name + '报告期成本' |
|
| 168 | ||
| 169 | reporting_period_data = report['reporting_period'] |
|
| 170 | category = report['offline_meter']['energy_category_name'] |
|
| 171 | ca_len = len(category) |
|
| 172 | ||
| 173 | ws['B7'].fill = table_fill |
|
| 174 | ||
| 175 | ws['B8'].font = title_font |
|
| 176 | ws['B8'].alignment = c_c_alignment |
|
| 177 | ws['B8'] = '成本' |
|
| 178 | ws['B8'].border = f_border |
|
| 179 | ||
| 180 | ws['B9'].font = title_font |
|
| 181 | ws['B9'].alignment = c_c_alignment |
|
| 182 | ws['B9'] = '环比' |
|
| 183 | ws['B9'].border = f_border |
|
| 184 | ||
| 185 | col = 'B' |
|
| 186 | ||
| 187 | for i in range(0, ca_len): |
|
| 188 | col = chr(ord('C') + i) |
|
| 189 | ||
| 190 | ws[col + '7'].fill = table_fill |
|
| 191 | ws[col + '7'].font = name_font |
|
| 192 | ws[col + '7'].alignment = c_c_alignment |
|
| 193 | ws[col + '7'] = report['offline_meter']['energy_category_name'] + \ |
|
| 194 | " (" + report['offline_meter']['unit_of_measure'] + ")" |
|
| 195 | ws[col + '7'].border = f_border |
|
| 196 | ||
| 197 | ws[col + '8'].font = name_font |
|
| 198 | ws[col + '8'].alignment = c_c_alignment |
|
| 199 | ws[col + '8'] = round(reporting_period_data['total_in_category'], 0) |
|
| 200 | ws[col + '8'].border = f_border |
|
| 201 | ||
| 202 | ws[col + '9'].font = name_font |
|
| 203 | ws[col + '9'].alignment = c_c_alignment |
|
| 204 | ws[col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
|
| 205 | if reporting_period_data['increment_rate'] is not None else "-" |
|
| 206 | ws[col + '9'].border = f_border |
|
| 207 | ||
| 208 | # TCE TCO2E |
|
| 209 | end_col = col |
|
| 210 | # TCE |
|
| 211 | tce_col = chr(ord(end_col) + 1) |
|
| 212 | ws[tce_col + '7'].fill = table_fill |
|
| 213 | ws[tce_col + '7'].font = name_font |
|
| 214 | ws[tce_col + '7'].alignment = c_c_alignment |
|
| 215 | ws[tce_col + '7'] = "TCE" |
|
| 216 | ws[tce_col + '7'].border = f_border |
|
| 217 | ||
| 218 | ws[tce_col + '8'].font = name_font |
|
| 219 | ws[tce_col + '8'].alignment = c_c_alignment |
|
| 220 | ws[tce_col + '8'] = round(reporting_period_data['total_in_kgce'], 0) |
|
| 221 | ws[tce_col + '8'].border = f_border |
|
| 222 | ||
| 223 | ws[tce_col + '9'].font = name_font |
|
| 224 | ws[tce_col + '9'].alignment = c_c_alignment |
|
| 225 | ws[tce_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
|
| 226 | if reporting_period_data['increment_rate'] is not None else "-" |
|
| 227 | ws[tce_col + '9'].border = f_border |
|
| 228 | ||
| 229 | # TCO2E |
|
| 230 | tco2e_col = chr(ord(end_col) + 2) |
|
| 231 | ws[tco2e_col + '7'].fill = table_fill |
|
| 232 | ws[tco2e_col + '7'].font = name_font |
|
| 233 | ws[tco2e_col + '7'].alignment = c_c_alignment |
|
| 234 | ws[tco2e_col + '7'] = "TCO2E" |
|
| 235 | ws[tco2e_col + '7'].border = f_border |
|
| 236 | ||
| 237 | ws[tco2e_col + '8'].font = name_font |
|
| 238 | ws[tco2e_col + '8'].alignment = c_c_alignment |
|
| 239 | ws[tco2e_col + '8'] = round(reporting_period_data['total_in_kgco2e'], 0) |
|
| 240 | ws[tco2e_col + '8'].border = f_border |
|
| 241 | ||
| 242 | ws[tco2e_col + '9'].font = name_font |
|
| 243 | ws[tco2e_col + '9'].alignment = c_c_alignment |
|
| 244 | ws[tco2e_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
|
| 245 | if reporting_period_data['increment_rate'] is not None else "-" |
|
| 246 | ws[tco2e_col + '9'].border = f_border |
|
| 247 | ||
| 248 | else: |
|
| 249 | for i in range(6, 9 + 1): |
|
| 250 | ws.rows_dimensions[i].height = 0.1 |
|
| 251 | ||
| 252 | ###################################### |
|
| 253 | ||
| 254 | has_cost_detail_flag = True |
|
| 255 | reporting_period_data = report['reporting_period'] |
|
| 256 | category = report['offline_meter']['energy_category_name'] |
|
| 257 | ca_len = len(category) |
|
| 258 | times = reporting_period_data['timestamps'] |
|
| 259 | ||
| 260 | if "values" not in reporting_period_data.keys() or len(reporting_period_data['values']) == 0: |
|
| 261 | has_cost_detail_flag = False |
|
| 262 | ||
| 263 | if has_cost_detail_flag: |
|
| 264 | ws['B11'].font = title_font |
|
| 265 | ws['B11'] = name + '详细数据' |
|
| 266 | ||
| 267 | ws['B18'].fill = table_fill |
|
| 268 | ws['B18'].font = title_font |
|
| 269 | ws['B18'].border = f_border |
|
| 270 | ws['B18'].alignment = c_c_alignment |
|
| 271 | ws['B18'] = '日期时间' |
|
| 272 | time = times |
|
| 273 | has_data = False |
|
| 274 | max_row = 0 |
|
| 275 | if len(time) > 0: |
|
| 276 | has_data = True |
|
| 277 | max_row = 18 + len(time) |
|
| 278 | ||
| 279 | if has_data: |
|
| 280 | ||
| 281 | end_data_flag = 19 |
|
| 282 | ||
| 283 | for i in range(0, len(time)): |
|
| 284 | col = 'B' |
|
| 285 | end_data_flag = 19 + i |
|
| 286 | row = str(end_data_flag) |
|
| 287 | ||
| 288 | ws[col + row].font = title_font |
|
| 289 | ws[col + row].alignment = c_c_alignment |
|
| 290 | ws[col + row] = time[i] |
|
| 291 | ws[col + row].border = f_border |
|
| 292 | ||
| 293 | ws['B' + str(end_data_flag + 1)].font = title_font |
|
| 294 | ws['B' + str(end_data_flag + 1)].alignment = c_c_alignment |
|
| 295 | ws['B' + str(end_data_flag + 1)] = '总计' |
|
| 296 | ws['B' + str(end_data_flag + 1)].border = f_border |
|
| 297 | ||
| 298 | bar = BarChart() |
|
| 299 | ||
| 300 | for i in range(0, ca_len): |
|
| 301 | ||
| 302 | col = chr(ord('C') + i) |
|
| 303 | ||
| 304 | ws[col + '18'].fill = table_fill |
|
| 305 | ws[col + '18'].font = title_font |
|
| 306 | ws[col + '18'].alignment = c_c_alignment |
|
| 307 | ws[col + '18'] = report['offline_meter']['energy_category_name'] + \ |
|
| 308 | " (" + report['offline_meter']['unit_of_measure'] + ")" |
|
| 309 | ws[col + '18'].border = f_border |
|
| 310 | ||
| 311 | time = times |
|
| 312 | time_len = len(time) |
|
| 313 | ||
| 314 | for j in range(0, time_len): |
|
| 315 | row = str(19 + j) |
|
| 316 | ||
| 317 | ws[col + row].font = title_font |
|
| 318 | ws[col + row].alignment = c_c_alignment |
|
| 319 | ws[col + row] = round(reporting_period_data['values'][j], 0) |
|
| 320 | ws[col + row].border = f_border |
|
| 321 | ||
| 322 | ws[col + str(end_data_flag + 1)].font = title_font |
|
| 323 | ws[col + str(end_data_flag + 1)].alignment = c_c_alignment |
|
| 324 | ws[col + str(end_data_flag + 1)] = round(reporting_period_data['total_in_category'], 0) |
|
| 325 | ws[col + str(end_data_flag + 1)].border = f_border |
|
| 326 | ||
| 327 | bar_data = Reference(ws, min_col=3 + i, min_row=18, max_row=max_row) |
|
| 328 | bar.series.append(Series(bar_data, title_from_data=True)) |
|
| 329 | ||
| 330 | labels = Reference(ws, min_col=2, min_row=19, max_row=max_row) |
|
| 331 | bar.set_categories(labels) |
|
| 332 | bar.dLbls = DataLabelList() |
|
| 333 | bar.dLbls.showVal = True |
|
| 334 | bar.height = 5.25 |
|
| 335 | bar.width = len(time) |
|
| 336 | ws.add_chart(bar, "B12") |
|
| 337 | else: |
|
| 338 | for i in range(11, 43 + 1): |
|
| 339 | ws.row_dimensions[i].height = 0.0 |
|
| 340 | ||
| 341 | filename = str(uuid.uuid4()) + '.xlsx' |
|
| 342 | wb.save(filename) |
|
| 343 | ||
| 344 | return filename |
|
| 345 | ||
| @@ 59-342 (lines=284) @@ | ||
| 56 | return base64_message |
|
| 57 | ||
| 58 | ||
| 59 | def generate_excel(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type): |
|
| 60 | wb = Workbook() |
|
| 61 | ||
| 62 | # todo |
|
| 63 | ws = wb.active |
|
| 64 | ||
| 65 | # Row height |
|
| 66 | ws.row_dimensions[1].height = 118 |
|
| 67 | for i in range(2, 11 + 1): |
|
| 68 | ws.row_dimensions[i].height = 30 |
|
| 69 | ||
| 70 | for i in range(12, 43 + 1): |
|
| 71 | ws.row_dimensions[i].height = 30 |
|
| 72 | ||
| 73 | # Col width |
|
| 74 | ws.column_dimensions['A'].width = 1.5 |
|
| 75 | ||
| 76 | for i in range(ord('B'), ord('I')): |
|
| 77 | ws.column_dimensions[chr(i)].width = 15.0 |
|
| 78 | ||
| 79 | # Font |
|
| 80 | name_font = Font(name='Constantia', size=15, bold=True) |
|
| 81 | title_font = Font(name='宋体', size=15, bold=True) |
|
| 82 | data_font = Font(name='Franklin Gothic Book', size=11) |
|
| 83 | ||
| 84 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
|
| 85 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
|
| 86 | right=Side(border_style='medium', color='00000000'), |
|
| 87 | bottom=Side(border_style='medium', color='00000000'), |
|
| 88 | top=Side(border_style='medium', color='00000000') |
|
| 89 | ) |
|
| 90 | b_border = Border( |
|
| 91 | bottom=Side(border_style='medium', color='00000000'), |
|
| 92 | ) |
|
| 93 | ||
| 94 | b_c_alignment = Alignment(vertical='bottom', |
|
| 95 | horizontal='center', |
|
| 96 | text_rotation=0, |
|
| 97 | wrap_text=False, |
|
| 98 | shrink_to_fit=False, |
|
| 99 | indent=0) |
|
| 100 | c_c_alignment = Alignment(vertical='center', |
|
| 101 | horizontal='center', |
|
| 102 | text_rotation=0, |
|
| 103 | wrap_text=False, |
|
| 104 | shrink_to_fit=False, |
|
| 105 | indent=0) |
|
| 106 | b_r_alignment = Alignment(vertical='bottom', |
|
| 107 | horizontal='right', |
|
| 108 | text_rotation=0, |
|
| 109 | wrap_text=False, |
|
| 110 | shrink_to_fit=False, |
|
| 111 | indent=0) |
|
| 112 | c_r_alignment = Alignment(vertical='bottom', |
|
| 113 | horizontal='center', |
|
| 114 | text_rotation=0, |
|
| 115 | wrap_text=False, |
|
| 116 | shrink_to_fit=False, |
|
| 117 | indent=0) |
|
| 118 | ||
| 119 | # Img |
|
| 120 | img = Image("excelexporters/myems.png") |
|
| 121 | ws.add_image(img, 'B1') |
|
| 122 | ||
| 123 | # Title |
|
| 124 | ws['B3'].font = name_font |
|
| 125 | ws['B3'].alignment = b_r_alignment |
|
| 126 | ws['B3'] = 'Name:' |
|
| 127 | ws['C3'].border = b_border |
|
| 128 | ws['C3'].alignment = b_c_alignment |
|
| 129 | ws['C3'].font = name_font |
|
| 130 | ws['C3'] = name |
|
| 131 | ||
| 132 | ws['D3'].font = name_font |
|
| 133 | ws['D3'].alignment = b_r_alignment |
|
| 134 | ws['D3'] = 'Period:' |
|
| 135 | ws['E3'].border = b_border |
|
| 136 | ws['E3'].alignment = b_c_alignment |
|
| 137 | ws['E3'].font = name_font |
|
| 138 | ws['E3'] = period_type |
|
| 139 | ||
| 140 | ws['F3'].font = name_font |
|
| 141 | ws['F3'].alignment = b_r_alignment |
|
| 142 | ws['F3'] = 'Date:' |
|
| 143 | ws.merge_cells("G3:H3") |
|
| 144 | ws['G3'].border = b_border |
|
| 145 | ws['G3'].alignment = b_c_alignment |
|
| 146 | ws['G3'].font = name_font |
|
| 147 | ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
|
| 148 | ||
| 149 | if "reporting_period" not in report.keys() or \ |
|
| 150 | "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
|
| 151 | filename = str(uuid.uuid4()) + '.xlsx' |
|
| 152 | wb.save(filename) |
|
| 153 | ||
| 154 | return filename |
|
| 155 | ||
| 156 | ############################### |
|
| 157 | ||
| 158 | has_cost_data_flag = True |
|
| 159 | ||
| 160 | if "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
|
| 161 | has_cost_data_flag = False |
|
| 162 | ||
| 163 | if has_cost_data_flag: |
|
| 164 | ws['B6'].font = title_font |
|
| 165 | ws['B6'] = name + '报告期消耗' |
|
| 166 | ||
| 167 | reporting_period_data = report['reporting_period'] |
|
| 168 | category = report['offline_meter']['energy_category_name'] |
|
| 169 | ca_len = len(category) |
|
| 170 | ||
| 171 | ws['B7'].fill = table_fill |
|
| 172 | ||
| 173 | ws['B8'].font = title_font |
|
| 174 | ws['B8'].alignment = c_c_alignment |
|
| 175 | ws['B8'] = '能耗' |
|
| 176 | ws['B8'].border = f_border |
|
| 177 | ||
| 178 | ws['B9'].font = title_font |
|
| 179 | ws['B9'].alignment = c_c_alignment |
|
| 180 | ws['B9'] = '环比' |
|
| 181 | ws['B9'].border = f_border |
|
| 182 | ||
| 183 | col = 'B' |
|
| 184 | ||
| 185 | for i in range(0, ca_len): |
|
| 186 | col = chr(ord('C') + i) |
|
| 187 | ||
| 188 | ws[col + '7'].fill = table_fill |
|
| 189 | ws[col + '7'].font = name_font |
|
| 190 | ws[col + '7'].alignment = c_c_alignment |
|
| 191 | ws[col + '7'] = report['offline_meter']['energy_category_name'] + \ |
|
| 192 | " (" + report['offline_meter']['unit_of_measure'] + ")" |
|
| 193 | ws[col + '7'].border = f_border |
|
| 194 | ||
| 195 | ws[col + '8'].font = name_font |
|
| 196 | ws[col + '8'].alignment = c_c_alignment |
|
| 197 | ws[col + '8'] = round(reporting_period_data['total_in_category'], 0) |
|
| 198 | ws[col + '8'].border = f_border |
|
| 199 | ||
| 200 | ws[col + '9'].font = name_font |
|
| 201 | ws[col + '9'].alignment = c_c_alignment |
|
| 202 | ws[col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
|
| 203 | if reporting_period_data['increment_rate'] is not None else "-" |
|
| 204 | ws[col + '9'].border = f_border |
|
| 205 | ||
| 206 | # TCE TCO2E |
|
| 207 | end_col = col |
|
| 208 | # TCE |
|
| 209 | tce_col = chr(ord(end_col) + 1) |
|
| 210 | ws[tce_col + '7'].fill = table_fill |
|
| 211 | ws[tce_col + '7'].font = name_font |
|
| 212 | ws[tce_col + '7'].alignment = c_c_alignment |
|
| 213 | ws[tce_col + '7'] = "TCE" |
|
| 214 | ws[tce_col + '7'].border = f_border |
|
| 215 | ||
| 216 | ws[tce_col + '8'].font = name_font |
|
| 217 | ws[tce_col + '8'].alignment = c_c_alignment |
|
| 218 | ws[tce_col + '8'] = round(reporting_period_data['total_in_kgce'], 0) |
|
| 219 | ws[tce_col + '8'].border = f_border |
|
| 220 | ||
| 221 | ws[tce_col + '9'].font = name_font |
|
| 222 | ws[tce_col + '9'].alignment = c_c_alignment |
|
| 223 | ws[tce_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
|
| 224 | if reporting_period_data['increment_rate'] is not None else "-" |
|
| 225 | ws[tce_col + '9'].border = f_border |
|
| 226 | ||
| 227 | # TCO2E |
|
| 228 | tco2e_col = chr(ord(end_col) + 2) |
|
| 229 | ws[tco2e_col + '7'].fill = table_fill |
|
| 230 | ws[tco2e_col + '7'].font = name_font |
|
| 231 | ws[tco2e_col + '7'].alignment = c_c_alignment |
|
| 232 | ws[tco2e_col + '7'] = "TCO2E" |
|
| 233 | ws[tco2e_col + '7'].border = f_border |
|
| 234 | ||
| 235 | ws[tco2e_col + '8'].font = name_font |
|
| 236 | ws[tco2e_col + '8'].alignment = c_c_alignment |
|
| 237 | ws[tco2e_col + '8'] = round(reporting_period_data['total_in_kgco2e'], 0) |
|
| 238 | ws[tco2e_col + '8'].border = f_border |
|
| 239 | ||
| 240 | ws[tco2e_col + '9'].font = name_font |
|
| 241 | ws[tco2e_col + '9'].alignment = c_c_alignment |
|
| 242 | ws[tco2e_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
|
| 243 | if reporting_period_data['increment_rate'] is not None else "-" |
|
| 244 | ws[tco2e_col + '9'].border = f_border |
|
| 245 | ||
| 246 | else: |
|
| 247 | for i in range(6, 9 + 1): |
|
| 248 | ws.rows_dimensions[i].height = 0.1 |
|
| 249 | ||
| 250 | ###################################### |
|
| 251 | ||
| 252 | has_cost_detail_flag = True |
|
| 253 | reporting_period_data = report['reporting_period'] |
|
| 254 | category = report['offline_meter']['energy_category_name'] |
|
| 255 | ca_len = len(category) |
|
| 256 | times = reporting_period_data['timestamps'] |
|
| 257 | ||
| 258 | if "values" not in reporting_period_data.keys() or len(reporting_period_data['values']) == 0: |
|
| 259 | has_cost_detail_flag = False |
|
| 260 | ||
| 261 | if has_cost_detail_flag: |
|
| 262 | ws['B11'].font = title_font |
|
| 263 | ws['B11'] = name + '详细数据' |
|
| 264 | ||
| 265 | ws['B18'].fill = table_fill |
|
| 266 | ws['B18'].font = title_font |
|
| 267 | ws['B18'].border = f_border |
|
| 268 | ws['B18'].alignment = c_c_alignment |
|
| 269 | ws['B18'] = '日期时间' |
|
| 270 | time = times |
|
| 271 | has_data = False |
|
| 272 | max_row = 0 |
|
| 273 | if len(time) > 0: |
|
| 274 | has_data = True |
|
| 275 | max_row = 18 + len(time) |
|
| 276 | ||
| 277 | if has_data: |
|
| 278 | ||
| 279 | end_data_flag = 19 |
|
| 280 | ||
| 281 | for i in range(0, len(time)): |
|
| 282 | col = 'B' |
|
| 283 | end_data_flag = 19 + i |
|
| 284 | row = str(end_data_flag) |
|
| 285 | ||
| 286 | ws[col + row].font = title_font |
|
| 287 | ws[col + row].alignment = c_c_alignment |
|
| 288 | ws[col + row] = time[i] |
|
| 289 | ws[col + row].border = f_border |
|
| 290 | ||
| 291 | ws['B' + str(end_data_flag + 1)].font = title_font |
|
| 292 | ws['B' + str(end_data_flag + 1)].alignment = c_c_alignment |
|
| 293 | ws['B' + str(end_data_flag + 1)] = '总计' |
|
| 294 | ws['B' + str(end_data_flag + 1)].border = f_border |
|
| 295 | ||
| 296 | bar = BarChart() |
|
| 297 | ||
| 298 | for i in range(0, ca_len): |
|
| 299 | ||
| 300 | col = chr(ord('C') + i) |
|
| 301 | ||
| 302 | ws[col + '18'].fill = table_fill |
|
| 303 | ws[col + '18'].font = title_font |
|
| 304 | ws[col + '18'].alignment = c_c_alignment |
|
| 305 | ws[col + '18'] = report['offline_meter']['energy_category_name'] + \ |
|
| 306 | " (" + report['offline_meter']['unit_of_measure'] + ")" |
|
| 307 | ws[col + '18'].border = f_border |
|
| 308 | ||
| 309 | time = times |
|
| 310 | time_len = len(time) |
|
| 311 | ||
| 312 | for j in range(0, time_len): |
|
| 313 | row = str(19 + j) |
|
| 314 | ||
| 315 | ws[col + row].font = title_font |
|
| 316 | ws[col + row].alignment = c_c_alignment |
|
| 317 | ws[col + row] = round(reporting_period_data['values'][j], 0) |
|
| 318 | ws[col + row].border = f_border |
|
| 319 | ||
| 320 | ws[col + str(end_data_flag + 1)].font = title_font |
|
| 321 | ws[col + str(end_data_flag + 1)].alignment = c_c_alignment |
|
| 322 | ws[col + str(end_data_flag + 1)] = round(reporting_period_data['total_in_category'], 0) |
|
| 323 | ws[col + str(end_data_flag + 1)].border = f_border |
|
| 324 | ||
| 325 | bar_data = Reference(ws, min_col=3 + i, min_row=18, max_row=max_row) |
|
| 326 | bar.series.append(Series(bar_data, title_from_data=True)) |
|
| 327 | ||
| 328 | labels = Reference(ws, min_col=2, min_row=19, max_row=max_row) |
|
| 329 | bar.set_categories(labels) |
|
| 330 | bar.dLbls = DataLabelList() |
|
| 331 | bar.dLbls.showVal = True |
|
| 332 | bar.height = 5.25 |
|
| 333 | bar.width = len(time) |
|
| 334 | ws.add_chart(bar, "B12") |
|
| 335 | else: |
|
| 336 | for i in range(11, 43 + 1): |
|
| 337 | ws.row_dimensions[i].height = 0.0 |
|
| 338 | ||
| 339 | filename = str(uuid.uuid4()) + '.xlsx' |
|
| 340 | wb.save(filename) |
|
| 341 | ||
| 342 | return filename |
|
| 343 | ||
| @@ 59-341 (lines=283) @@ | ||
| 56 | return base64_message |
|
| 57 | ||
| 58 | ||
| 59 | def generate_excel(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type): |
|
| 60 | wb = Workbook() |
|
| 61 | # todo |
|
| 62 | ws = wb.active |
|
| 63 | ||
| 64 | # Row height |
|
| 65 | ws.row_dimensions[1].height = 118 |
|
| 66 | for i in range(2, 11 + 1): |
|
| 67 | ws.row_dimensions[i].height = 30 |
|
| 68 | ||
| 69 | for i in range(12, 43 + 1): |
|
| 70 | ws.row_dimensions[i].height = 30 |
|
| 71 | ||
| 72 | # Col width |
|
| 73 | ws.column_dimensions['A'].width = 1.5 |
|
| 74 | ||
| 75 | for i in range(ord('B'), ord('I')): |
|
| 76 | ws.column_dimensions[chr(i)].width = 15.0 |
|
| 77 | ||
| 78 | # Font |
|
| 79 | name_font = Font(name='Constantia', size=15, bold=True) |
|
| 80 | title_font = Font(name='宋体', size=15, bold=True) |
|
| 81 | data_font = Font(name='Franklin Gothic Book', size=11) |
|
| 82 | ||
| 83 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
|
| 84 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
|
| 85 | right=Side(border_style='medium', color='00000000'), |
|
| 86 | bottom=Side(border_style='medium', color='00000000'), |
|
| 87 | top=Side(border_style='medium', color='00000000') |
|
| 88 | ) |
|
| 89 | b_border = Border( |
|
| 90 | bottom=Side(border_style='medium', color='00000000'), |
|
| 91 | ) |
|
| 92 | ||
| 93 | b_c_alignment = Alignment(vertical='bottom', |
|
| 94 | horizontal='center', |
|
| 95 | text_rotation=0, |
|
| 96 | wrap_text=False, |
|
| 97 | shrink_to_fit=False, |
|
| 98 | indent=0) |
|
| 99 | c_c_alignment = Alignment(vertical='center', |
|
| 100 | horizontal='center', |
|
| 101 | text_rotation=0, |
|
| 102 | wrap_text=False, |
|
| 103 | shrink_to_fit=False, |
|
| 104 | indent=0) |
|
| 105 | b_r_alignment = Alignment(vertical='bottom', |
|
| 106 | horizontal='right', |
|
| 107 | text_rotation=0, |
|
| 108 | wrap_text=False, |
|
| 109 | shrink_to_fit=False, |
|
| 110 | indent=0) |
|
| 111 | c_r_alignment = Alignment(vertical='bottom', |
|
| 112 | horizontal='center', |
|
| 113 | text_rotation=0, |
|
| 114 | wrap_text=False, |
|
| 115 | shrink_to_fit=False, |
|
| 116 | indent=0) |
|
| 117 | ||
| 118 | # Img |
|
| 119 | img = Image("excelexporters/myems.png") |
|
| 120 | ws.add_image(img, 'B1') |
|
| 121 | ||
| 122 | # Title |
|
| 123 | ws['B3'].font = name_font |
|
| 124 | ws['B3'].alignment = b_r_alignment |
|
| 125 | ws['B3'] = 'Name:' |
|
| 126 | ws['C3'].border = b_border |
|
| 127 | ws['C3'].alignment = b_c_alignment |
|
| 128 | ws['C3'].font = name_font |
|
| 129 | ws['C3'] = name |
|
| 130 | ||
| 131 | ws['D3'].font = name_font |
|
| 132 | ws['D3'].alignment = b_r_alignment |
|
| 133 | ws['D3'] = 'Period:' |
|
| 134 | ws['E3'].border = b_border |
|
| 135 | ws['E3'].alignment = b_c_alignment |
|
| 136 | ws['E3'].font = name_font |
|
| 137 | ws['E3'] = period_type |
|
| 138 | ||
| 139 | ws['F3'].font = name_font |
|
| 140 | ws['F3'].alignment = b_r_alignment |
|
| 141 | ws['F3'] = 'Date:' |
|
| 142 | ws.merge_cells("G3:H3") |
|
| 143 | ws['G3'].border = b_border |
|
| 144 | ws['G3'].alignment = b_c_alignment |
|
| 145 | ws['G3'].font = name_font |
|
| 146 | ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
|
| 147 | ||
| 148 | if "reporting_period" not in report.keys() or \ |
|
| 149 | "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
|
| 150 | filename = str(uuid.uuid4()) + '.xlsx' |
|
| 151 | wb.save(filename) |
|
| 152 | ||
| 153 | return filename |
|
| 154 | ||
| 155 | ############################### |
|
| 156 | ||
| 157 | has_cost_data_flag = True |
|
| 158 | ||
| 159 | if "values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
|
| 160 | has_cost_data_flag = False |
|
| 161 | ||
| 162 | if has_cost_data_flag: |
|
| 163 | ws['B6'].font = title_font |
|
| 164 | ws['B6'] = name + '报告期消耗' |
|
| 165 | ||
| 166 | reporting_period_data = report['reporting_period'] |
|
| 167 | category = report['virtual_meter']['energy_category_name'] |
|
| 168 | ca_len = len(category) |
|
| 169 | ||
| 170 | ws['B7'].fill = table_fill |
|
| 171 | ||
| 172 | ws['B8'].font = title_font |
|
| 173 | ws['B8'].alignment = c_c_alignment |
|
| 174 | ws['B8'] = '能耗' |
|
| 175 | ws['B8'].border = f_border |
|
| 176 | ||
| 177 | ws['B9'].font = title_font |
|
| 178 | ws['B9'].alignment = c_c_alignment |
|
| 179 | ws['B9'] = '环比' |
|
| 180 | ws['B9'].border = f_border |
|
| 181 | ||
| 182 | col = 'B' |
|
| 183 | ||
| 184 | for i in range(0, ca_len): |
|
| 185 | col = chr(ord('C') + i) |
|
| 186 | ||
| 187 | ws[col + '7'].fill = table_fill |
|
| 188 | ws[col + '7'].font = name_font |
|
| 189 | ws[col + '7'].alignment = c_c_alignment |
|
| 190 | ws[col + '7'] = report['virtual_meter']['energy_category_name'] + \ |
|
| 191 | " (" + report['virtual_meter']['unit_of_measure'] + ")" |
|
| 192 | ws[col + '7'].border = f_border |
|
| 193 | ||
| 194 | ws[col + '8'].font = name_font |
|
| 195 | ws[col + '8'].alignment = c_c_alignment |
|
| 196 | ws[col + '8'] = round(reporting_period_data['total_in_category'], 0) |
|
| 197 | ws[col + '8'].border = f_border |
|
| 198 | ||
| 199 | ws[col + '9'].font = name_font |
|
| 200 | ws[col + '9'].alignment = c_c_alignment |
|
| 201 | ws[col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
|
| 202 | if reporting_period_data['increment_rate'] is not None else "-" |
|
| 203 | ws[col + '9'].border = f_border |
|
| 204 | ||
| 205 | # TCE TCO2E |
|
| 206 | end_col = col |
|
| 207 | # TCE |
|
| 208 | tce_col = chr(ord(end_col) + 1) |
|
| 209 | ws[tce_col + '7'].fill = table_fill |
|
| 210 | ws[tce_col + '7'].font = name_font |
|
| 211 | ws[tce_col + '7'].alignment = c_c_alignment |
|
| 212 | ws[tce_col + '7'] = "TCE" |
|
| 213 | ws[tce_col + '7'].border = f_border |
|
| 214 | ||
| 215 | ws[tce_col + '8'].font = name_font |
|
| 216 | ws[tce_col + '8'].alignment = c_c_alignment |
|
| 217 | ws[tce_col + '8'] = round(reporting_period_data['total_in_kgce'], 0) |
|
| 218 | ws[tce_col + '8'].border = f_border |
|
| 219 | ||
| 220 | ws[tce_col + '9'].font = name_font |
|
| 221 | ws[tce_col + '9'].alignment = c_c_alignment |
|
| 222 | ws[tce_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
|
| 223 | if reporting_period_data['increment_rate'] is not None else "-" |
|
| 224 | ws[tce_col + '9'].border = f_border |
|
| 225 | ||
| 226 | # TCO2E |
|
| 227 | tco2e_col = chr(ord(end_col) + 2) |
|
| 228 | ws[tco2e_col + '7'].fill = table_fill |
|
| 229 | ws[tco2e_col + '7'].font = name_font |
|
| 230 | ws[tco2e_col + '7'].alignment = c_c_alignment |
|
| 231 | ws[tco2e_col + '7'] = "TCO2E" |
|
| 232 | ws[tco2e_col + '7'].border = f_border |
|
| 233 | ||
| 234 | ws[tco2e_col + '8'].font = name_font |
|
| 235 | ws[tco2e_col + '8'].alignment = c_c_alignment |
|
| 236 | ws[tco2e_col + '8'] = round(reporting_period_data['total_in_kgco2e'], 0) |
|
| 237 | ws[tco2e_col + '8'].border = f_border |
|
| 238 | ||
| 239 | ws[tco2e_col + '9'].font = name_font |
|
| 240 | ws[tco2e_col + '9'].alignment = c_c_alignment |
|
| 241 | ws[tco2e_col + '9'] = str(round(reporting_period_data['increment_rate'] * 100, 2)) + "%" \ |
|
| 242 | if reporting_period_data['increment_rate'] is not None else "-" |
|
| 243 | ws[tco2e_col + '9'].border = f_border |
|
| 244 | ||
| 245 | else: |
|
| 246 | for i in range(6, 9 + 1): |
|
| 247 | ws.rows_dimensions[i].height = 0.1 |
|
| 248 | ||
| 249 | ###################################### |
|
| 250 | ||
| 251 | has_cost_detail_flag = True |
|
| 252 | reporting_period_data = report['reporting_period'] |
|
| 253 | category = report['virtual_meter']['energy_category_name'] |
|
| 254 | ca_len = len(category) |
|
| 255 | times = reporting_period_data['timestamps'] |
|
| 256 | ||
| 257 | if "values" not in reporting_period_data.keys() or len(reporting_period_data['values']) == 0: |
|
| 258 | has_cost_detail_flag = False |
|
| 259 | ||
| 260 | if has_cost_detail_flag: |
|
| 261 | ws['B11'].font = title_font |
|
| 262 | ws['B11'] = name + '详细数据' |
|
| 263 | ||
| 264 | ws['B18'].fill = table_fill |
|
| 265 | ws['B18'].font = title_font |
|
| 266 | ws['B18'].border = f_border |
|
| 267 | ws['B18'].alignment = c_c_alignment |
|
| 268 | ws['B18'] = '日期时间' |
|
| 269 | time = times |
|
| 270 | has_data = False |
|
| 271 | max_row = 0 |
|
| 272 | if len(time) > 0: |
|
| 273 | has_data = True |
|
| 274 | max_row = 18 + len(time) |
|
| 275 | ||
| 276 | if has_data: |
|
| 277 | ||
| 278 | end_data_flag = 19 |
|
| 279 | ||
| 280 | for i in range(0, len(time)): |
|
| 281 | col = 'B' |
|
| 282 | end_data_flag = 19 + i |
|
| 283 | row = str(end_data_flag) |
|
| 284 | ||
| 285 | ws[col + row].font = title_font |
|
| 286 | ws[col + row].alignment = c_c_alignment |
|
| 287 | ws[col + row] = time[i] |
|
| 288 | ws[col + row].border = f_border |
|
| 289 | ||
| 290 | ws['B' + str(end_data_flag + 1)].font = title_font |
|
| 291 | ws['B' + str(end_data_flag + 1)].alignment = c_c_alignment |
|
| 292 | ws['B' + str(end_data_flag + 1)] = '总计' |
|
| 293 | ws['B' + str(end_data_flag + 1)].border = f_border |
|
| 294 | ||
| 295 | bar = BarChart() |
|
| 296 | ||
| 297 | for i in range(0, ca_len): |
|
| 298 | ||
| 299 | col = chr(ord('C') + i) |
|
| 300 | ||
| 301 | ws[col + '18'].fill = table_fill |
|
| 302 | ws[col + '18'].font = title_font |
|
| 303 | ws[col + '18'].alignment = c_c_alignment |
|
| 304 | ws[col + '18'] = report['virtual_meter']['energy_category_name'] + \ |
|
| 305 | " (" + report['virtual_meter']['unit_of_measure'] + ")" |
|
| 306 | ws[col + '18'].border = f_border |
|
| 307 | ||
| 308 | time = times |
|
| 309 | time_len = len(time) |
|
| 310 | ||
| 311 | for j in range(0, time_len): |
|
| 312 | row = str(19 + j) |
|
| 313 | ||
| 314 | ws[col + row].font = title_font |
|
| 315 | ws[col + row].alignment = c_c_alignment |
|
| 316 | ws[col + row] = round(reporting_period_data['values'][j], 0) |
|
| 317 | ws[col + row].border = f_border |
|
| 318 | ||
| 319 | ws[col + str(end_data_flag + 1)].font = title_font |
|
| 320 | ws[col + str(end_data_flag + 1)].alignment = c_c_alignment |
|
| 321 | ws[col + str(end_data_flag + 1)] = round(reporting_period_data['total_in_category'], 0) |
|
| 322 | ws[col + str(end_data_flag + 1)].border = f_border |
|
| 323 | ||
| 324 | bar_data = Reference(ws, min_col=3 + i, min_row=18, max_row=max_row) |
|
| 325 | bar.series.append(Series(bar_data, title_from_data=True)) |
|
| 326 | ||
| 327 | labels = Reference(ws, min_col=2, min_row=19, max_row=max_row) |
|
| 328 | bar.set_categories(labels) |
|
| 329 | bar.dLbls = DataLabelList() |
|
| 330 | bar.dLbls.showVal = True |
|
| 331 | bar.height = 5.25 |
|
| 332 | bar.width = len(time) |
|
| 333 | ws.add_chart(bar, "B12") |
|
| 334 | else: |
|
| 335 | for i in range(11, 43 + 1): |
|
| 336 | ws.row_dimensions[i].height = 0.0 |
|
| 337 | ||
| 338 | filename = str(uuid.uuid4()) + '.xlsx' |
|
| 339 | wb.save(filename) |
|
| 340 | ||
| 341 | return filename |
|
| 342 | ||