Python Forum
openpyx (ValueError: Style Consolas exists already) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: openpyx (ValueError: Style Consolas exists already) (/thread-36448.html)



openpyx (ValueError: Style Consolas exists already) - Irv1n - Feb-21-2022

My code:
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).style = self.consolas
        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).style = self.consolas
            return self.wb.save(wb_name)
if i try to add data in existing sheet i have error:
self.xl.col_writer(self.cal_hat, self.data_meas, self.xlsx_path, self.sh_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