Skip to content

Commit 7202635

Browse files
jfrfonsecajreback
authored andcommitted
Inclusion of new NAMED_COLORS for MS Excel (#18392)
1 parent 402de2e commit 7202635

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

pandas/io/formats/excel.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@ def build_font(self, props):
277277

278278
NAMED_COLORS = {
279279
'maroon': '800000',
280+
'brown': 'A52A2A',
280281
'red': 'FF0000',
282+
'pink': 'FFC0CB',
281283
'orange': 'FFA500',
282284
'yellow': 'FFFF00',
283285
'olive': '808000',
@@ -291,6 +293,7 @@ def build_font(self, props):
291293
'navy': '000080',
292294
'black': '000000',
293295
'gray': '808080',
296+
'grey': '808080',
294297
'silver': 'C0C0C0',
295298
'white': 'FFFFFF',
296299
}

pandas/tests/io/formats/test_to_excel.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"""
55

66
import pytest
7+
import pandas.util.testing as tm
78

9+
from warnings import catch_warnings
810
from pandas.io.formats.excel import CSSToExcelConverter
911

1012

@@ -212,3 +214,61 @@ def test_css_to_excel_multiple():
212214
def test_css_to_excel_inherited(css, inherited, expected):
213215
convert = CSSToExcelConverter(inherited)
214216
assert expected == convert(css)
217+
218+
219+
@pytest.mark.parametrize("input_color,output_color", (
220+
[(name, rgb) for name, rgb in CSSToExcelConverter.NAMED_COLORS.items()] +
221+
[("#" + rgb, rgb) for rgb in CSSToExcelConverter.NAMED_COLORS.values()] +
222+
[("#F0F", "FF00FF"), ("#ABC", "AABBCC")])
223+
)
224+
def test_css_to_excel_good_colors(input_color, output_color):
225+
# see gh-18392
226+
css = ("border-top-color: {color}; "
227+
"border-right-color: {color}; "
228+
"border-bottom-color: {color}; "
229+
"border-left-color: {color}; "
230+
"background-color: {color}; "
231+
"color: {color}").format(color=input_color)
232+
233+
expected = dict()
234+
235+
expected["fill"] = {
236+
"patternType": "solid",
237+
"fgColor": output_color
238+
}
239+
240+
expected["font"] = {
241+
"color": output_color
242+
}
243+
244+
expected["border"] = {
245+
k: {
246+
"color": output_color,
247+
} for k in ("top", "right", "bottom", "left")
248+
}
249+
250+
with tm.assert_produces_warning(None):
251+
convert = CSSToExcelConverter()
252+
assert expected == convert(css)
253+
254+
255+
@pytest.mark.parametrize("input_color", [None, "not-a-color"])
256+
def test_css_to_excel_bad_colors(input_color):
257+
# see gh-18392
258+
css = ("border-top-color: {color}; "
259+
"border-right-color: {color}; "
260+
"border-bottom-color: {color}; "
261+
"border-left-color: {color}; "
262+
"background-color: {color}; "
263+
"color: {color}").format(color=input_color)
264+
265+
expected = dict()
266+
267+
if input_color is not None:
268+
expected["fill"] = {
269+
"patternType": "solid"
270+
}
271+
272+
with catch_warnings(record=True):
273+
convert = CSSToExcelConverter()
274+
assert expected == convert(css)

0 commit comments

Comments
 (0)