be able to write python's datetime

This commit is contained in:
chfw 2015-01-24 23:09:12 +00:00
parent 52977e4546
commit 844cd5dee4
5 changed files with 30 additions and 9 deletions

View File

@ -193,6 +193,12 @@ You need to pass a StringIO instance to Writer::
>>> # object for downloading
Known Issues
=============
* If a zero was typed in a DATE formatted field in xls, you will get "01/01/1900".
* If a zero was typed in a TIME formatted field in xls, you will get "00:00:00".
Dependencies
============

View File

@ -18,6 +18,11 @@ else:
from collections import OrderedDict
DEFAULT_DATE_FORMAT = "DD/MM/YY"
DEFAULT_TIME_FORMAT = "HH:MM:SS"
DEFAULT_DATETIME_FORMAT = "%s %s" % (DEFAULT_DATE_FORMAT, DEFAULT_TIME_FORMAT)
XLS_FORMAT_CONVERSION = {
xlrd.XL_CELL_TEXT: str,
xlrd.XL_CELL_EMPTY: None,
@ -65,6 +70,7 @@ class XLSheet(SheetReader):
"""
@property
def name(self):
"""This is required by :class:`SheetReader`"""
return self.native_sheet.name
def number_of_rows(self):
@ -98,15 +104,19 @@ class XLBook(BookReader):
It reads xls, xlsm, xlsx work book
"""
def sheetIterator(self):
"""Return iterable sheet array"""
return self.native_book.sheets()
def getSheet(self, native_sheet):
"""Create a xls sheet"""
return XLSheet(native_sheet)
def load_from_memory(self, file_content):
"""Provide the way to load xls from memory"""
return xlrd.open_workbook(None, file_contents=file_content)
def load_from_file(self, filename):
"""Provide the way to load xls from a file"""
return xlrd.open_workbook(filename)
@ -115,6 +125,8 @@ class XLSheetWriter(SheetWriter):
xls, xlsx and xlsm sheet writer
"""
def set_sheet_name(self, name):
"""Create a sheet
"""
self.native_sheet = self.native_book.add_sheet(name)
self.current_row = 0
@ -127,22 +139,23 @@ class XLSheetWriter(SheetWriter):
style = None
tmp_array = []
if isinstance(value, datetime.datetime):
tmp_array = [value.year, value.month, value.day,
value.hour, value.minute, value.second
tmp_array = [
value.year, value.month, value.day,
value.hour, value.minute, value.second
]
value = xlrd.xldate.xldate_from_datetime_tuple(tmp_array, 0)
style = XFStyle()
style.num_format_str = "DD/MM/YY HH:MM:SS"
style.num_format_str = DEFAULT_DATETIME_FORMAT
elif isinstance(value, datetime.date):
tmp_array = [value.year, value.month, value.day]
value = xlrd.xldate.xldate_from_date_tuple(tmp_array, 0)
style = XFStyle()
style.num_format_str = "DD/MM/YY"
style.num_format_str = DEFAULT_DATE_FORMAT
elif isinstance(value, datetime.time):
tmp_array = [value.hour, value.minute, value.second]
value = xlrd.xldate.xldate_from_time_tuple(tmp_array)
style = XFStyle()
style.num_format_str = "HH:MM:SS"
style.num_format_str = DEFAULT_TIME_FORMAT
if style:
self.native_sheet.write(self.current_row, i, value, style)
else:
@ -155,10 +168,12 @@ class XLWriter(BookWriter):
xls, xlsx and xlsm writer
"""
def __init__(self, file, **keywords):
"""Initialize a xlwt work book"""
BookWriter.__init__(self, file, **keywords)
self.wb = Workbook()
def create_sheet(self, name):
"""Create a xlwt writer"""
return XLSheetWriter(self.wb, None, name)
def close(self):
@ -183,4 +198,4 @@ except:
# to allow this module to function independently
pass
__VERSION__ = "0.0.3"
__VERSION__ = "0.0.4"

View File

@ -1 +1 @@
nosetests --with-cov --with-doctest --doctest-extension=.rst
nosetests --with-cov --cov pyexcel_xls --cov tests --with-doctest --doctest-extension=.rst

View File

@ -1 +1 @@
nosetests --rednose --with-cov --with-doctest --doctest-extension=.rst
nosetests --rednose --with-cov --cov pyexcel_xls --cov tests --with-doctest --doctest-extension=.rst

View File

@ -35,5 +35,5 @@ class TestDateFormat:
assert isinstance(r[0,1], datetime.time) == True
assert r[0,1].strftime("%H:%M:%S") == "11:11:11"
assert isinstance(r[0,2], datetime.date) == True
assert r[0,2].strftime("%d/%m/%y") == "25/12/14"
assert r[0,2].strftime("%d/%m/%y %H:%M:%S") == "25/12/14 11:11:11"
os.unlink(excel_filename)