Skip to content

Commit 8069f47

Browse files
committed
Merge pull request #8356 from jreback/excel_fix
BUG: Work-around openpyxl-2.1.0 NumberFormat removal
2 parents 1d65bc8 + 66d6912 commit 8069f47

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

pandas/io/excel.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def read_excel(io, sheetname=0, **kwds):
7171
7272
Parameters
7373
----------
74-
io : string, file-like object, or xlrd workbook.
74+
io : string, file-like object, or xlrd workbook.
7575
The string could be a URL. Valid URL schemes include http, ftp, s3,
7676
and file. For file URLs, a host is expected. For instance, a local
7777
file could be file://localhost/path/to/workbook.xlsx
@@ -166,7 +166,7 @@ def __init__(self, io, **kwds):
166166
self.book = io
167167
elif not isinstance(io, xlrd.Book) and hasattr(io, "read"):
168168
# N.B. xlrd.Book has a read attribute too
169-
data = io.read()
169+
data = io.read()
170170
self.book = xlrd.open_workbook(file_contents=data)
171171
else:
172172
raise ValueError('Must explicitly set engine if not passing in'
@@ -1029,21 +1029,24 @@ def _convert_to_alignment(cls, alignment_dict):
10291029
@classmethod
10301030
def _convert_to_number_format(cls, number_format_dict):
10311031
"""
1032-
Convert ``number_format_dict`` to an openpyxl v2 NumberFormat object.
1032+
Convert ``number_format_dict`` to an openpyxl v2.1.0 number format
1033+
initializer.
10331034
Parameters
10341035
----------
10351036
number_format_dict : dict
10361037
A dict with zero or more of the following keys.
1037-
'format_code'
1038+
'format_code' : str
10381039
Returns
10391040
-------
1040-
number_format : openpyxl.styles.NumberFormat
1041+
number_format : str
10411042
"""
1042-
1043-
from openpyxl.styles import NumberFormat
1044-
1045-
return NumberFormat(**number_format_dict)
1046-
1043+
try:
1044+
# >= 2.0.0 < 2.1.0
1045+
from openpyxl.styles import NumberFormat
1046+
return NumberFormat(**number_format_dict)
1047+
except:
1048+
# >= 2.1.0
1049+
return number_format_dict['format_code']
10471050

10481051
@classmethod
10491052
def _convert_to_protection(cls, protection_dict):

pandas/io/tests/test_excel.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,7 @@ def test_to_excel_styleconverter(self):
11981198
if not openpyxl_compat.is_compat(major_ver=2):
11991199
raise nose.SkipTest('incompatiable openpyxl version')
12001200

1201+
import openpyxl
12011202
from openpyxl import styles
12021203

12031204
hstyle = {
@@ -1238,7 +1239,14 @@ def test_to_excel_styleconverter(self):
12381239
alignment = styles.Alignment(horizontal='center', vertical='top')
12391240
fill_color = styles.Color(rgb='006666FF', tint=0.3)
12401241
fill = styles.PatternFill(patternType='solid', fgColor=fill_color)
1241-
number_format = styles.NumberFormat(format_code='0.00')
1242+
1243+
# ahh openpyxl API changes
1244+
ver = openpyxl.__version__
1245+
if ver >= LooseVersion('2.0.0') and ver < LooseVersion('2.1.0'):
1246+
number_format = styles.NumberFormat(format_code='0.00')
1247+
else:
1248+
number_format = '0.00' # XXX: Only works with openpyxl-2.1.0
1249+
12421250
protection = styles.Protection(locked=True, hidden=False)
12431251

12441252
kw = _Openpyxl2Writer._convert_to_style_kwargs(hstyle)

0 commit comments

Comments
 (0)