Skip to content

BUG: Work-around openpyxl-2.1.0 NumberFormat removal #8356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions pandas/io/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 9 additions & 1 deletion pandas/io/tests/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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)
Expand Down