Conditions | 4 |
Total Lines | 109 |
Code Lines | 85 |
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 |
||
54 | def generate_excel(report, space_name): |
||
55 | |||
56 | wb = Workbook() |
||
57 | ws = wb.active |
||
58 | |||
59 | # Row height |
||
60 | ws.row_dimensions[1].height = 118 |
||
61 | for i in range(2, 5000 + 1): |
||
62 | ws.row_dimensions[i].height = 30 |
||
63 | # Col width |
||
64 | ws.column_dimensions['A'].width = 1 |
||
65 | |||
66 | # Font |
||
67 | name_font = Font(name='Constantia', size=15, bold=True) |
||
68 | title_font = Font(name='宋体', size=15, bold=True) |
||
69 | data_font = Font(name='Franklin Gothic Book', size=11) |
||
70 | |||
71 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
||
72 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
||
73 | right=Side(border_style='medium', color='00000000'), |
||
74 | bottom=Side(border_style='medium', color='00000000'), |
||
75 | top=Side(border_style='medium', color='00000000') |
||
76 | ) |
||
77 | b_border = Border( |
||
78 | bottom=Side(border_style='medium', color='00000000'), |
||
79 | ) |
||
80 | |||
81 | b_c_alignment = Alignment(vertical='bottom', |
||
82 | horizontal='center', |
||
83 | text_rotation=0, |
||
84 | wrap_text=False, |
||
85 | shrink_to_fit=False, |
||
86 | indent=0) |
||
87 | c_c_alignment = Alignment(vertical='center', |
||
88 | horizontal='center', |
||
89 | text_rotation=0, |
||
90 | wrap_text=False, |
||
91 | shrink_to_fit=False, |
||
92 | indent=0) |
||
93 | b_r_alignment = Alignment(vertical='bottom', |
||
94 | horizontal='right', |
||
95 | text_rotation=0, |
||
96 | wrap_text=False, |
||
97 | shrink_to_fit=False, |
||
98 | indent=0) |
||
99 | c_r_alignment = Alignment(vertical='bottom', |
||
100 | horizontal='center', |
||
101 | text_rotation=0, |
||
102 | wrap_text=False, |
||
103 | shrink_to_fit=False, |
||
104 | indent=0) |
||
105 | for i in range(ord('B'), ord('F')): |
||
106 | ws.column_dimensions[chr(i)].width = 30.0 |
||
107 | |||
108 | # Img |
||
109 | ws.merge_cells("B1:D1") |
||
110 | ws.merge_cells("B2:E2") |
||
111 | img = Image("excelexporters/myems.png") |
||
112 | ws.add_image(img, 'B1') |
||
113 | |||
114 | # Title |
||
115 | ws['B3'].border = f_border |
||
116 | ws['B3'].font = name_font |
||
117 | ws['B3'].alignment = b_c_alignment |
||
118 | ws['B3'] = '名称' |
||
119 | |||
120 | ws['C3'].border = f_border |
||
121 | ws['C3'].alignment = b_c_alignment |
||
122 | ws['C3'].font = name_font |
||
123 | ws['C3'] = '空间' |
||
124 | |||
125 | ws['D3'].border = f_border |
||
126 | ws['D3'].font = name_font |
||
127 | ws['D3'].alignment = b_c_alignment |
||
128 | ws['D3'] = '成本中心' |
||
129 | |||
130 | ws['E3'].border = f_border |
||
131 | ws['E3'].alignment = b_c_alignment |
||
132 | ws['E3'].font = name_font |
||
133 | ws['E3'] = '描述' |
||
134 | |||
135 | current_row_number = 4 |
||
136 | for i in range(0, len(report['equipments'])): |
||
137 | |||
138 | ws['B' + str(current_row_number)].font = title_font |
||
139 | ws['B' + str(current_row_number)].border = f_border |
||
140 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
141 | ws['B' + str(current_row_number)] = report['equipments'][i]['equipment_name'] |
||
142 | |||
143 | ws['C' + str(current_row_number)].font = title_font |
||
144 | ws['C' + str(current_row_number)].border = f_border |
||
145 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
146 | ws['C' + str(current_row_number)] = report['equipments'][i]['space_name'] |
||
147 | |||
148 | ws['D' + str(current_row_number)].font = title_font |
||
149 | ws['D' + str(current_row_number)].border = f_border |
||
150 | ws['D' + str(current_row_number)].alignment = c_c_alignment |
||
151 | ws['D' + str(current_row_number)] = report['equipments'][i]['cost_center_name'] |
||
152 | |||
153 | ws['E' + str(current_row_number)].font = title_font |
||
154 | ws['E' + str(current_row_number)].border = f_border |
||
155 | ws['E' + str(current_row_number)].alignment = c_c_alignment |
||
156 | ws['E' + str(current_row_number)] = report['equipments'][i]['description'] |
||
157 | current_row_number += 1 |
||
158 | |||
159 | filename = str(uuid.uuid4()) + '.xlsx' |
||
160 | wb.save(filename) |
||
161 | |||
162 | return filename |
||
163 |