diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py index 44c7be853b6a0..292602feb01dc 100644 --- a/pandas/io/parsers.py +++ b/pandas/io/parsers.py @@ -21,6 +21,8 @@ import pandas.lib as lib import pandas._parser as _parser from pandas.tseries.period import Period +import json + class DateConversionError(Exception): pass @@ -1929,7 +1931,7 @@ class CellStyleConverter(object): """ @staticmethod - def to_xls(style_dict): + def to_xls(style_dict, num_format_str=None): """ converts a style_dict to an xlwt style object Parameters @@ -1971,9 +1973,13 @@ def style_to_xlwt(item, firstlevel=True, field_sep=',', line_sep=';'): if style_dict: xlwt_stylestr = style_to_xlwt(style_dict) - return xlwt.easyxf(xlwt_stylestr, field_sep=',', line_sep=';') + style = xlwt.easyxf(xlwt_stylestr, field_sep=',', line_sep=';') else: - return xlwt.XFStyle() + style = xlwt.XFStyle() + if num_format_str is not None: + style.num_format_str = num_format_str + + return style @staticmethod def to_xlsx(style_dict): @@ -2111,13 +2117,26 @@ def _writecells_xls(self, cells, sheet_name, startrow, startcol): wks = self.book.add_sheet(sheet_name) self.sheets[sheet_name] = wks + style_dict = {} + for cell in cells: val = _conv_value(cell.val) - style = CellStyleConverter.to_xls(cell.style) - if isinstance(val, datetime.datetime): - style.num_format_str = "YYYY-MM-DD HH:MM:SS" - elif isinstance(val, datetime.date): - style.num_format_str = "YYYY-MM-DD" + + num_format_str = None + if isinstance(cell.val, datetime.datetime): + num_format_str = "YYYY-MM-DD HH:MM:SS" + if isinstance(cell.val, datetime.date): + num_format_str = "YYYY-MM-DD" + + stylekey = json.dumps(cell.style) + if num_format_str: + stylekey += num_format_str + + if stylekey in style_dict: + style = style_dict[stylekey] + else: + style = CellStyleConverter.to_xls(cell.style, num_format_str) + style_dict[stylekey] = style if cell.mergestart is not None and cell.mergeend is not None: wks.write_merge(startrow + cell.row,