First of all just want to say thanks for provided an excellent set of
excel read/write tools. I am involved in the Cardsharp oss project <a
href="
http://cardsharp.norc.org/cardsharp/wiki/Home">Cardsharp</a> and
we are using the python-excel libraries for our excel loader.
The issue I am having is that when using xlutils.copy() date,
datetime, and time formats are not preserved. I think the core issue
has to do with the xf_index being incorrectly set. When debugging the
datetime objects have an xf_index of 17 instead of 18 - 20.
I fixed this issue by implementing the below monkey-patch on
xlutils.filter.cell and xlwt.Row.set_cell_number.
Am I missing something/is there a way to preserve the datetime objects
without applying the monkey-patch?
Thanks!
Mike
def my_set_cell_number(self, colx, number,
style=xlwt.Style.default_style, is_date=False):
self._Row__adjust_height(style)
self._Row__adjust_bound_col_idx(colx)
xf_index = self._Row__parent_wb.add_style(style)
if is_date:
if 1 > number >= 0:
xf_index = 20
elif number - int(number) == 0:
xf_index = 19
else:
xf_index = 18
self.insert_cell(colx, xlwt.Cell.NumberCell(self._Row__idx, colx,
xf_index, number))
def my_cell(self,rdrowx,rdcolx,wtrowx,wtcolx):
cell = self.rdsheet.cell(rdrowx,rdcolx)
# setup column attributes if not already set
if wtcolx not in self.wtcols and rdcolx in
self.rdsheet.colinfo_map:
rdcol = self.rdsheet.colinfo_map[rdcolx]
wtcol = self.wtsheet.col(wtcolx)
wtcol.width = rdcol.width
wtcol.set_style(self.style_list[rdcol.xf_index])
wtcol.hidden = rdcol.hidden
wtcol.level = rdcol.outline_level
wtcol.collapsed = rdcol.collapsed
self.wtcols.add(wtcolx)
# copy cell
cty = cell.ctype
if cty == xlrd.XL_CELL_EMPTY:
return
if cell.xf_index is not None:
style = self.style_list[cell.xf_index]
else:
style = default_style
rdcoords2d = (rdrowx, rdcolx)
if rdcoords2d in self.merged_cell_top_left_map:
# The cell is the governing cell of a group of
# merged cells.
rlo, rhi, clo, chi = self.merged_cell_top_left_map[rdcoords2d]
assert (rlo, clo) == rdcoords2d
self.wtsheet.write_merge(
wtrowx, wtrowx + rhi - rlo - 1,
wtcolx, wtcolx + chi - clo - 1,
cell.value, style)
return
if rdcoords2d in self.merged_cell_already_set:
# The cell is in a group of merged cells.
# It has been handled by the write_merge() call above.
# We avoid writing a record again because:
# (1) It's a waste of CPU time and disk space.
# (2) xlwt does not (as at 2007-01-12) ensure that only
# the last record is written to the file.
# (3) If you write a data record for a cell
# followed by a blank record for the same cell,
# Excel will display a blank but OOo Calc and
# Gnumeric will display the data :-(
return
x = 0
wtrow = self.wtsheet.row(wtrowx)
if cty == xlrd.XL_CELL_TEXT:
wtrow.set_cell_text(wtcolx, cell.value, style)
elif cty == xlrd.XL_CELL_NUMBER:
wtrow.set_cell_number(wtcolx, cell.value, style)
elif cty == xlrd.XL_CELL_DATE:
wtrow.set_cell_number(wtcolx, cell.value, style, is_date=True)
elif cty == xlrd.XL_CELL_BLANK:
wtrow.set_cell_blank(wtcolx, style)
elif cty == xlrd.XL_CELL_BOOLEAN:
wtrow.set_cell_boolean(wtcolx, cell.value, style)
elif cty == xlrd.XL_CELL_ERROR:
wtrow.set_cell_error(wtcolx, cell.value, style)
else:
raise Exception(
"Unknown xlrd cell type %r with value %r at (shx=%r,rowx=
%r,colx=%r)" \
% (cty, value, sheetx, rowx, colx)
)
--
You received this message because you are subscribed to the Google Groups "python-excel" group.
To post to this group, send an email to
python...@googlegroups.com.
To unsubscribe from this group, send email to
python-excel...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/python-excel?hl=en-GB.