From 57a596d7ff1e29d34cc958092f5d2af638238235 Mon Sep 17 00:00:00 2001 From: locojaydev Date: Thu, 6 Dec 2012 18:19:42 -0500 Subject: [PATCH 1/2] large files --- pandas/io/parsers.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py index 44c7be853b6a0..258f129f0cd56 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 @@ -2111,14 +2113,24 @@ 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) + + stylekey = json.dumps(cell.style) + if stylekey in style_dict: + style = style_dict[stylekey] + else: + style = CellStyleConverter.to_xls(cell.style) + style_dict[stylekey] = 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" + if cell.mergestart is not None and cell.mergeend is not None: wks.write_merge(startrow + cell.row, startrow + cell.mergestart, From a750958963361656a2a3cca618ba6259f27b6f1c Mon Sep 17 00:00:00 2001 From: locojaydev Date: Fri, 7 Dec 2012 11:25:58 -0500 Subject: [PATCH 2/2] test fix --- pandas/io/parsers.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py index 258f129f0cd56..292602feb01dc 100644 --- a/pandas/io/parsers.py +++ b/pandas/io/parsers.py @@ -1931,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 @@ -1973,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): @@ -2118,19 +2122,22 @@ def _writecells_xls(self, cells, sheet_name, startrow, startcol): for cell in cells: val = _conv_value(cell.val) + 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) + style = CellStyleConverter.to_xls(cell.style, num_format_str) style_dict[stylekey] = 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" - - if cell.mergestart is not None and cell.mergeend is not None: wks.write_merge(startrow + cell.row, startrow + cell.mergestart,