Wichert.
In reality, there are no such things. What you have are floating point numbers and pious hope. There are several problems with Excel dates:
(1) Dates are not stored as a separate data type; they are stored as floating point numbers and you have to rely on (a) the "number format" applied to them in Excel and/or (b) knowing which cells are supposed to have dates in them. This module helps with (a) by inspecting the format that has been applied to each number cell; if it appears to be a date format, the cell is classified as a date rather than a number.Why doesn't xlwd (and xlrd) do that automatically? I can't think of a
reason not to do that.
Wichert.
--
Wichert Akkerman <wic...@wiggy.net> It is simple to make things.
http://www.wiggy.net/ It is hard to make things simple.
--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/python-excel?hl=en-GB.
But isn't any default better than a number? Right now there is no way to
tell that it even is a date.
Wichert.
DEFAULT_STYLE = xlwt.XFStyle()DATE_STYLE = xlwt.XFStyle()DATE_STYLE.num_format_str = 'M/D/YY'def default_column_style(val):if isinstance(val, (datetime.date, datetime.datetime)):return DATE_STYLEreturn DEFAULT_STYLE...
ws.write(r, c, val, default_column_style(val))
But you can just do this yourself:
DEFAULT_STYLE = xlwt.XFStyle()DATE_STYLE = xlwt.XFStyle()DATE_STYLE.num_format_str = 'M/D/YY'
def default_column_style(val):if isinstance(val, (datetime.date, datetime.datetime)):return DATE_STYLEreturn DEFAULT_STYLE
...
ws.write(r, c, val, default_column_style(val))
Then you can control exactly what style you want to use for date or any other type.
--
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.
Why doesn't xlwd (and xlrd) do that automatically? I can't think of a
reason not to do that.
People want to get date/datetime/time objects as the cell value.
I think that's a reasonable expectation, but I remember you being
opposed to this?
cheers,
Chris
--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
I can't speak for John Machin of course, but I personally don't mind
that xlrd doesn't do more with dates. It does tell you if it thinks a
cell is a date, and it provides xldate_as_tuple so if you trust that
the cell is indeed a date, you can create a Python date object rather
easily. On the other hand, if xlrd were to always give you a date
object for such cells, and what you really wanted was the raw value,
it is trickier to get the raw value back.
What I think the OP is really after is not more xlrd functionality,
but rather a default date style to be applied automatically in xlwt
when writing a date value with no explicit style. Personally, I find
this quite reasonable. OP is willing to accept that the default date
format won't necessarily be locale-appropriate, just that it be
clearly recognizable (i.e. human-readable) as a date, so I would go
with ISO.
(Where appropriate, "date" above refers to date, datetime, or time.)
John Y.
--
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+unsubscribe@googlegroups.com.
I think you have a misunderstanding of what is actually going on.
Python doesn't autoformat anything, and xlrd and xlwt certainly don't
autoformat anything.
You said the "next app" receives a CSV file... but what is this next
app, and how does it interpret CSV fields? (CSV fields don't
*inherently* have any formatting, though some applications such as
Excel will *assume* formatting when reading CSVs.)
You said you had to resave the file from Excel... so what did the
dates look like in Excel, and what kind of file did you save to?
If you were to get date/datetime/time objects (which you can do very
easily, by the way), what would you do with them, and how would that
help?
John Y.