diff --git a/pandas/io/excel.py b/pandas/io/excel.py index 84f04188b7906..dcecbedd87dcf 100644 --- a/pandas/io/excel.py +++ b/pandas/io/excel.py @@ -71,7 +71,7 @@ def read_excel(io, sheetname=0, **kwds): Parameters ---------- - io : string, file-like object, or xlrd workbook. + io : string, file-like object, or xlrd workbook. The string could be a URL. Valid URL schemes include http, ftp, s3, and file. For file URLs, a host is expected. For instance, a local file could be file://localhost/path/to/workbook.xlsx @@ -166,7 +166,7 @@ def __init__(self, io, **kwds): self.book = io elif not isinstance(io, xlrd.Book) and hasattr(io, "read"): # N.B. xlrd.Book has a read attribute too - data = io.read() + data = io.read() self.book = xlrd.open_workbook(file_contents=data) else: raise ValueError('Must explicitly set engine if not passing in' @@ -1029,21 +1029,24 @@ def _convert_to_alignment(cls, alignment_dict): @classmethod def _convert_to_number_format(cls, number_format_dict): """ - Convert ``number_format_dict`` to an openpyxl v2 NumberFormat object. + Convert ``number_format_dict`` to an openpyxl v2.1.0 number format + initializer. Parameters ---------- number_format_dict : dict A dict with zero or more of the following keys. - 'format_code' + 'format_code' : str Returns ------- - number_format : openpyxl.styles.NumberFormat + number_format : str """ - - from openpyxl.styles import NumberFormat - - return NumberFormat(**number_format_dict) - + try: + # >= 2.0.0 < 2.1.0 + from openpyxl.styles import NumberFormat + return NumberFormat(**number_format_dict) + except: + # >= 2.1.0 + return number_format_dict['format_code'] @classmethod def _convert_to_protection(cls, protection_dict): diff --git a/pandas/io/tests/test_excel.py b/pandas/io/tests/test_excel.py index 17407e3a864e2..662f12da8c4d0 100644 --- a/pandas/io/tests/test_excel.py +++ b/pandas/io/tests/test_excel.py @@ -1198,6 +1198,7 @@ def test_to_excel_styleconverter(self): if not openpyxl_compat.is_compat(major_ver=2): raise nose.SkipTest('incompatiable openpyxl version') + import openpyxl from openpyxl import styles hstyle = { @@ -1238,7 +1239,14 @@ def test_to_excel_styleconverter(self): alignment = styles.Alignment(horizontal='center', vertical='top') fill_color = styles.Color(rgb='006666FF', tint=0.3) fill = styles.PatternFill(patternType='solid', fgColor=fill_color) - number_format = styles.NumberFormat(format_code='0.00') + + # ahh openpyxl API changes + ver = openpyxl.__version__ + if ver >= LooseVersion('2.0.0') and ver < LooseVersion('2.1.0'): + number_format = styles.NumberFormat(format_code='0.00') + else: + number_format = '0.00' # XXX: Only works with openpyxl-2.1.0 + protection = styles.Protection(locked=True, hidden=False) kw = _Openpyxl2Writer._convert_to_style_kwargs(hstyle)