Skip to content

Commit 61fdc69

Browse files
committed
Complete testing basic CSS -> Excel conversions
1 parent 6ff8a46 commit 61fdc69

File tree

2 files changed

+69
-10
lines changed

2 files changed

+69
-10
lines changed

pandas/formats/excel.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ def build_alignment(self, props):
125125
# TODO: text-indent, padding-left -> alignment.indent
126126
return {'horizontal': props.get('text-align'),
127127
'vertical': self.VERTICAL_MAP.get(props.get('vertical-align')),
128-
'wrap_text': (props['white-space'] not in (None, 'nowrap')
129-
if 'white-space' in props else None),
128+
'wrap_text': (None if props.get('white-space') is None else
129+
props['white-space'] not in
130+
('nowrap', 'pre', 'pre-line'))
130131
}
131132

132133
def build_border(self, props):
@@ -162,9 +163,9 @@ def _border_style(self, style, width):
162163
width = float(width[:-2])
163164
if width < 1e-5:
164165
return None
165-
elif width < 2:
166+
elif width < 1.3:
166167
width_name = 'thin'
167-
elif width < 3.5:
168+
elif width < 2.8:
168169
width_name = 'medium'
169170
else:
170171
width_name = 'thick'

pandas/tests/formats/test_to_excel.py

+64-6
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,77 @@
8989
'bottom': {'style': 'medium'},
9090
'left': {'style': 'medium'},
9191
'right': {'style': 'medium'}}}),
92+
('border-style: solid; border-width: thin',
93+
{'border': {'top': {'style': 'thin'},
94+
'bottom': {'style': 'thin'},
95+
'left': {'style': 'thin'},
96+
'right': {'style': 'thin'}}}),
97+
98+
('border-top-style: solid; border-top-width: thin',
99+
{'border': {'top': {'style': 'thin'}}}),
100+
('border-top-style: solid; border-top-width: 1pt',
101+
{'border': {'top': {'style': 'thin'}}}),
92102
('border-top-style: solid',
93103
{'border': {'top': {'style': 'medium'}}}),
104+
('border-top-style: solid; border-top-width: medium',
105+
{'border': {'top': {'style': 'medium'}}}),
106+
('border-top-style: solid; border-top-width: 2pt',
107+
{'border': {'top': {'style': 'medium'}}}),
108+
('border-top-style: solid; border-top-width: thick',
109+
{'border': {'top': {'style': 'thick'}}}),
110+
('border-top-style: solid; border-top-width: 4pt',
111+
{'border': {'top': {'style': 'thick'}}}),
112+
94113
('border-top-style: dotted',
95114
{'border': {'top': {'style': 'mediumDashDotDot'}}}),
115+
('border-top-style: dotted; border-top-width: thin',
116+
{'border': {'top': {'style': 'dotted'}}}),
96117
('border-top-style: dashed',
97118
{'border': {'top': {'style': 'mediumDashed'}}}),
98-
# TODO: test other widths
119+
('border-top-style: dashed; border-top-width: thin',
120+
{'border': {'top': {'style': 'dashed'}}}),
121+
('border-top-style: double',
122+
{'border': {'top': {'style': 'double'}}}),
99123
# - color
100-
# TODO
124+
('border-style: solid; border-color: #0000ff',
125+
{'border': {'top': {'style': 'medium', 'color': '0000FF'},
126+
'right': {'style': 'medium', 'color': '0000FF'},
127+
'bottom': {'style': 'medium', 'color': '0000FF'},
128+
'left': {'style': 'medium', 'color': '0000FF'}}}),
129+
('border-top-style: double; border-top-color: blue',
130+
{'border': {'top': {'style': 'double', 'color': '0000FF'}}}),
131+
('border-top-style: solid; border-top-color: #06c',
132+
{'border': {'top': {'style': 'medium', 'color': '0066CC'}}}),
101133
# ALIGNMENT
102134
# - horizontal
103-
# TODO
135+
('text-align: center',
136+
{'alignment': {'horizontal': 'center'}}),
137+
('text-align: left',
138+
{'alignment': {'horizontal': 'left'}}),
139+
('text-align: right',
140+
{'alignment': {'horizontal': 'right'}}),
141+
('text-align: justify',
142+
{'alignment': {'horizontal': 'justify'}}),
104143
# - vertical
105-
# TODO
144+
('vertical-align: top',
145+
{'alignment': {'vertical': 'top'}}),
146+
('vertical-align: text-top',
147+
{'alignment': {'vertical': 'top'}}),
148+
('vertical-align: middle',
149+
{'alignment': {'vertical': 'center'}}),
150+
('vertical-align: bottom',
151+
{'alignment': {'vertical': 'bottom'}}),
152+
('vertical-align: text-bottom',
153+
{'alignment': {'vertical': 'bottom'}}),
106154
# - wrap_text
107-
# TODO
155+
('white-space: nowrap',
156+
{'alignment': {'wrap_text': False}}),
157+
('white-space: pre',
158+
{'alignment': {'wrap_text': False}}),
159+
('white-space: pre-line',
160+
{'alignment': {'wrap_text': False}}),
161+
('white-space: normal',
162+
{'alignment': {'wrap_text': True}}),
108163
])
109164
def test_css_to_excel(css, expected):
110165
convert = CSSToExcelConverter()
@@ -115,12 +170,15 @@ def test_css_to_excel_multiple():
115170
convert = CSSToExcelConverter()
116171
actual = convert('''
117172
font-weight: bold;
173+
text-decoration: underline;
174+
color: red;
118175
border-width: thin;
119176
text-align: center;
120177
vertical-align: top;
121178
unused: something;
122179
''')
123-
assert {"font": {"bold": True},
180+
assert {"font": {"bold": True, "strike": False,
181+
"underline": "single", "color": "FF0000"},
124182
"border": {"top": {"style": "thin"},
125183
"right": {"style": "thin"},
126184
"bottom": {"style": "thin"},

0 commit comments

Comments
 (0)