| Conditions | 7 |
| Total Lines | 149 |
| Code Lines | 115 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import base64 |
||
| 51 | def generate_excel(report, space_name, reporting_start_datetime_local, reporting_end_datetime_local): |
||
| 52 | |||
| 53 | wb = Workbook() |
||
| 54 | ws = wb.active |
||
| 55 | |||
| 56 | # Row height |
||
| 57 | ws.row_dimensions[1].height = 102 |
||
| 58 | for i in range(2, 5 + 1): |
||
| 59 | ws.row_dimensions[i].height = 42 |
||
| 60 | |||
| 61 | for i in range(6, len(report['tenants']) + 15): |
||
| 62 | ws.row_dimensions[i].height = 60 |
||
| 63 | |||
| 64 | # Col width |
||
| 65 | ws.column_dimensions['A'].width = 1.5 |
||
| 66 | |||
| 67 | ws.column_dimensions['B'].width = 25.0 |
||
| 68 | |||
| 69 | for i in range(ord('C'), ord('L')): |
||
| 70 | ws.column_dimensions[chr(i)].width = 15.0 |
||
| 71 | |||
| 72 | # Font |
||
| 73 | name_font = Font(name='Constantia', size=15, bold=True) |
||
| 74 | title_font = Font(name='宋体', size=15, bold=True) |
||
| 75 | data_font = Font(name='Franklin Gothic Book', size=11) |
||
| 76 | |||
| 77 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
||
| 78 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
||
| 79 | right=Side(border_style='medium', color='00000000'), |
||
| 80 | bottom=Side(border_style='medium', color='00000000'), |
||
| 81 | top=Side(border_style='medium', color='00000000') |
||
| 82 | ) |
||
| 83 | b_border = Border( |
||
| 84 | bottom=Side(border_style='medium', color='00000000'), |
||
| 85 | ) |
||
| 86 | |||
| 87 | b_c_alignment = Alignment(vertical='bottom', |
||
| 88 | horizontal='center', |
||
| 89 | text_rotation=0, |
||
| 90 | wrap_text=True, |
||
| 91 | shrink_to_fit=False, |
||
| 92 | indent=0) |
||
| 93 | c_c_alignment = Alignment(vertical='center', |
||
| 94 | horizontal='center', |
||
| 95 | text_rotation=0, |
||
| 96 | wrap_text=True, |
||
| 97 | shrink_to_fit=False, |
||
| 98 | indent=0) |
||
| 99 | b_r_alignment = Alignment(vertical='bottom', |
||
| 100 | horizontal='right', |
||
| 101 | text_rotation=0, |
||
| 102 | wrap_text=True, |
||
| 103 | shrink_to_fit=False, |
||
| 104 | indent=0) |
||
| 105 | c_r_alignment = Alignment(vertical='bottom', |
||
| 106 | horizontal='center', |
||
| 107 | text_rotation=0, |
||
| 108 | wrap_text=True, |
||
| 109 | shrink_to_fit=False, |
||
| 110 | indent=0) |
||
| 111 | |||
| 112 | # Img |
||
| 113 | img = Image("excelexporters/myems.png") |
||
| 114 | img.width = img.width * 0.85 |
||
| 115 | img.height = img.height * 0.85 |
||
| 116 | ws.add_image(img, 'B1') |
||
| 117 | |||
| 118 | # Title |
||
| 119 | ws.row_dimensions[3].height = 60 |
||
| 120 | |||
| 121 | ws['B3'].font = name_font |
||
| 122 | ws['B3'].alignment = b_r_alignment |
||
| 123 | ws['B3'] = '空间:' |
||
| 124 | ws['C3'].border = b_border |
||
| 125 | ws['C3'].alignment = b_c_alignment |
||
| 126 | ws['C3'].font = name_font |
||
| 127 | ws['C3'] = space_name |
||
| 128 | |||
| 129 | ws['F3'].font = name_font |
||
| 130 | ws['F3'].alignment = b_r_alignment |
||
| 131 | ws['F3'] = '日期:' |
||
| 132 | ws['G3'].border = b_border |
||
| 133 | ws['G3'].alignment = b_c_alignment |
||
| 134 | ws['G3'].font = name_font |
||
| 135 | ws['G3'] = reporting_start_datetime_local + "~" + reporting_end_datetime_local |
||
| 136 | ws.merge_cells("G3:H3") |
||
| 137 | |||
| 138 | # Title |
||
| 139 | ws['B6'].border = f_border |
||
| 140 | ws['B6'].font = name_font |
||
| 141 | ws['B6'].alignment = c_c_alignment |
||
| 142 | ws['B6'].fill = table_fill |
||
| 143 | ws['B6'] = '名称' |
||
| 144 | |||
| 145 | ws['C6'].border = f_border |
||
| 146 | ws['C6'].alignment = c_c_alignment |
||
| 147 | ws['C6'].font = name_font |
||
| 148 | ws['C6'].fill = table_fill |
||
| 149 | ws['C6'] = '空间' |
||
| 150 | |||
| 151 | ws['D6'].border = f_border |
||
| 152 | ws['D6'].font = name_font |
||
| 153 | ws['D6'].alignment = c_c_alignment |
||
| 154 | ws['D6'].fill = table_fill |
||
| 155 | ws['D6'] = '成本中心' |
||
| 156 | |||
| 157 | ca_len = len(report['energycategories']) |
||
| 158 | |||
| 159 | for i in range(0, ca_len): |
||
| 160 | col = chr(ord('E') + i) |
||
| 161 | ws[col + '6'].fill = table_fill |
||
| 162 | ws[col + '6'].font = name_font |
||
| 163 | ws[col + '6'].alignment = c_c_alignment |
||
| 164 | ws[col + '6'] = report['energycategories'][i]['name'] + \ |
||
| 165 | " (" + report['energycategories'][i]['unit_of_measure'] + ")" |
||
| 166 | ws[col + '6'].border = f_border |
||
| 167 | |||
| 168 | current_row_number = 7 |
||
| 169 | for i in range(0, len(report['tenants'])): |
||
| 170 | |||
| 171 | ws['B' + str(current_row_number)].font = title_font |
||
| 172 | ws['B' + str(current_row_number)].border = f_border |
||
| 173 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
| 174 | ws['B' + str(current_row_number)] = report['tenants'][i]['tenant_name'] |
||
| 175 | |||
| 176 | ws['C' + str(current_row_number)].font = title_font |
||
| 177 | ws['C' + str(current_row_number)].border = f_border |
||
| 178 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
| 179 | ws['C' + str(current_row_number)] = report['tenants'][i]['space_name'] |
||
| 180 | |||
| 181 | ws['D' + str(current_row_number)].font = title_font |
||
| 182 | ws['D' + str(current_row_number)].border = f_border |
||
| 183 | ws['D' + str(current_row_number)].alignment = c_c_alignment |
||
| 184 | ws['D' + str(current_row_number)] = report['tenants'][i]['cost_center_name'] |
||
| 185 | |||
| 186 | ca_len = len(report['tenants'][i]['values']) |
||
| 187 | for j in range(0, ca_len): |
||
| 188 | col = chr(ord('E') + j) |
||
| 189 | ws[col + str(current_row_number)].font = title_font |
||
| 190 | ws[col + str(current_row_number)].border = f_border |
||
| 191 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
| 192 | ws[col + str(current_row_number)] = report['tenants'][i]['values'][j] |
||
| 193 | |||
| 194 | current_row_number += 1 |
||
| 195 | |||
| 196 | filename = str(uuid.uuid4()) + '.xlsx' |
||
| 197 | wb.save(filename) |
||
| 198 | |||
| 199 | return filename |
||
| 200 |