ValueError: Style Consolas exists already

361 views
Skip to first unread message

Vadim Popko

unread,
Feb 21, 2022, 6:58:06 AM2/21/22
to openpyxl-users
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)

and i have this error:
  File "d:\Documents\sample_editor\excel.py", line 48, in col_writer
    self.wb[ws_name].cell(r_index, c_index, row).style = self.consolas
  File "C:\Users\Popko\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\styles\styleable.py", line 78, in __set__
    instance.parent.parent.add_named_style(style)
  File "C:\Users\Popko\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\Popko\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

Charlie Clark

unread,
Feb 21, 2022, 8:57:16 AM2/21/22
to openpyxl-users

On 21 Feb 2022, at 12:58, Vadim Popko wrote:

> class Excel:
>
> def __init__(self):
>
> 	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')
>

Hi Vadim,

as per the bug report I think the problem is that you are essentially continually attempting to register a new named style. You should probably have a method that checks for the style in the workbook and adds it if necessary:

if "Consolas" not in wb.named_styles:
    wb.add_named_style()

You already have some code that does some checking so it probably makes sense to the check there.

Charlie

--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Sengelsweg 34
Düsseldorf
D- 40489
Tel: +49-203-3925-0390
Mobile: +49-178-782-6226

Miky Jhon

unread,
Feb 9, 2026, 2:05:23 PM (yesterday) Feb 9
to openpyxl-users
When working with openpyxl, Unicode handling often comes up, especially if you’re exporting styled text or user-generated labels into Excel. I’ve noticed that Excel generally preserves Unicode characters well, as long as the font supports them, which makes testing important. For experimenting with different Unicode symbols and fonts, tools like  are handy for quickly generating copy-paste text. It’s a simple way to validate how decorative Unicode strings behave inside spreadsheets.  
Reply all
Reply to author
Forward
0 new messages