class Excel: def __init__(self) -> None: self.wb = xl.Workbook() del self.wb['Sheet'] self.consolas = NamedStyle(name='Consolas') self.consolas.font = Font(name='Consolas', size=10) self.consolas.alignment = Alignment(horizontal='center',vertical='center') # self.wb.active # self.file_name = 'unknown.xlsx' def file_exist(self, wb_name, ws_name): # проверяет на существование файла if os.path.isfile(wb_name) is True: # если файл существует self.wb = load_workbook (wb_name) # то загружает его if ws_name in self.wb: self.wb.active else: self.wb.create_sheet(ws_name, 0) self.wb.active else: self.wb.create_sheet(ws_name, 0) self.wb.active self.wb.save(wb_name) return self.wb.save(wb_name) def last_column(self, wb_name, ws_name): self.wb = load_workbook (wb_name) self.ws = self.wb[ws_name] self.last_row = self.ws.max_row self.last_col = self.ws.max_column return self.last_col def col_writer(self, data_hat, data_meas, wb_name, ws_name): self.wb = load_workbook (wb_name) self.ws = self.wb[ws_name] self.last_row = self.ws.max_row self.last_col = self.ws.max_column for c_index, col in enumerate(data_hat, start=self.last_col+1): self.wb[ws_name].column_dimensions[get_column_letter(c_index)].width = 20 for r_index, row in enumerate(col, start=1): self.wb[ws_name].cell(r_index, c_index, row) for c_index, col in enumerate(data_meas, start=self.last_col+1): for r_index, row in enumerate(col, start=11): self.wb[ws_name].cell(r_index, c_index, row) return self.wb.save(wb_name)
File “d:\Documents\sample_editor\excel.py”, line 56, in col_writer
self.wb.add_named_style(self.consolas)
File “C:\Users\sss\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\workbook\workbook.py”, line 344, in add_named_style
self._named_styles.append(style)
File “C:\Users\sss\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\styles\named_styles.py”, line 193, in append
raise ValueError(“”“Style {0} exists already”“”.format(style.name))
ValueError: Style Consolas exists already