Skip to content
GitLab
Projects Groups Topics Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Register
  • Sign in
  • O openpyxl
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributor statistics
    • Graph
    • Compare revisions
  • Issues 282
    • Issues 282
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 7
    • Merge requests 7
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Container Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar

Due to a massive storm of spam, registration on this instance now requires explicit administrator approval. Sorry for the added friction, we're also looking into automatic filtering options.

  • openpyxl
  • openpyxl
  • Issues
  • #1786
Closed
Open
Issue created Feb 01, 2022 by Dave@damies13

Named Style overwriting previous Named Style

Not sure if this is me doing something wrongly or not understanding the documentation or actually a bug?

Here is a code example to reproduce the issue i'm having: [xlsx_style_issue.py]:

import openpyxl


def configure_style(wb):

	fontsize = 12
	highlightcolour = 'e34409'

	stylea = openpyxl.styles.NamedStyle(name="StyleA")
	stylea.font.name = 'Arial'
	stylea.font.size = fontsize*2
	wb.add_named_style(stylea)
	print("Checkpoint 1: StyleA:", stylea.name, stylea.font.name, stylea.font.size, stylea.alignment.horizontal, stylea.font.color)

	# According to the documentation here:
	# https://openpyxl.readthedocs.io/en/stable/styles.html#creating-a-named-style
	# this next line should create styleb as a new NamedStyle object?
	styleb = openpyxl.styles.NamedStyle(name="StyleB")
	styleb.font.name = 'Times New Roman'
	styleb.font.size = fontsize
	styleb.font.color = highlightcolour
	wb.add_named_style(styleb)
	print("Checkpoint 2: StyleB:", styleb.name, styleb.font.name, styleb.font.size, styleb.alignment.horizontal, styleb.font.color)
	print("Checkpoint 3: StyleA:", stylea.name, stylea.font.name, stylea.font.size, stylea.alignment.horizontal, stylea.font.color)


def create_xlsx():
	mywb = openpyxl.Workbook()

	configure_style(mywb)

	ws = mywb.active

	styleacell = ws.cell(column=2, row=3, value="This should be Arial 24 & black (default colour)")
	styleacell.style = "StyleA"

	stylebcell = ws.cell(column=2, row=5, value="This should be Times New Roman 12 & Orange")
	stylebcell.style = "StyleB"

	mywb.save("xlsx_style_issue.xlsx")


create_xlsx()

The print() outputs shows the issue is occurring in configure_style()

Checkpoint 1: StyleA: StyleA Arial 24.0 None None
Checkpoint 2: StyleB: StyleB Times New Roman 12.0 None <openpyxl.styles.colors.Color object>
Parameters:
rgb='00e34409', indexed=None, auto=None, theme=None, tint=0.0, type='rgb'
Checkpoint 3: StyleA: StyleA Times New Roman 12.0 None <openpyxl.styles.colors.Color object>
Parameters:
rgb='00e34409', indexed=None, auto=None, theme=None, tint=0.0, type='rgb'

Expected:

Checkpoint 3 and Checkpoint 1 should be the same, and Checkpoint 3 should be different to Checkpoint 2

Actual:

Checkpoint 3 is the same as Checkpoint 2

When you check the excel file, cells B3 and B5 look the same (both have the StyleB settings) even though they have different styles.

> pip3 show openpyxl
Name: openpyxl
Version: 3.0.9
Summary: A Python library to read/write Excel 2010 xlsx/xlsm files
Home-page: https://openpyxl.readthedocs.io
Author: See AUTHORS
Author-email: charlie.clark@clark-consulting.eu
License: MIT
Location: /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages
Requires: et-xmlfile
Required-by: 
Edited Feb 01, 2022 by CharlieC
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking