Python Forum
openpyx (ValueError: Style Consolas exists already)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
openpyx (ValueError: Style Consolas exists already)
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  p]Why os.path.exists("abc/d") and os.path.exists("abc/D") treat same rajeev1729 1 2,186 May-27-2020, 08:34 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020