diff --git a/pyexcel_libxlsxw/xlsxw.py b/pyexcel_libxlsxw/xlsxw.py index d49d93b..f43bcf1 100644 --- a/pyexcel_libxlsxw/xlsxw.py +++ b/pyexcel_libxlsxw/xlsxw.py @@ -7,6 +7,8 @@ :copyright: (c) 2016-2020 by Onni Software Ltd & its contributors :license: New BSD License """ +from datetime import date, time, datetime + from libxlsxwpy import Book from pyexcel_io.plugin_api import IWriter, ISheetWriter @@ -29,6 +31,39 @@ def write_row(self, array): self.xlsx_sheet.write_number(self.current_row, index, cell) elif isinstance(cell, bool): self.xlsx_sheet.write_boolean(self.current_row, index, cell) + elif isinstance(cell, datetime): + self.xlsx_sheet.write_datetime( + self.current_row, + index, + cell.year, + cell.month, + cell.day, + cell.hour, + cell.minute, + cell.second, + ) + elif isinstance(cell, date): + self.xlsx_sheet.write_datetime( + self.current_row, + index, + cell.year, + cell.month, + cell.day, + 0, + 0, + 0, + ) + elif isinstance(cell, time): + self.xlsx_sheet.write_datetime( + self.current_row, + index, + 0, + 0, + 0, + cell.hour, + cell.minute, + cell.second, + ) else: self.xlsx_sheet.write_string( self.current_row, index, str(cell) diff --git a/tests/base.py b/tests/base.py index 523b5ee..73ea2bf 100644 --- a/tests/base.py +++ b/tests/base.py @@ -11,7 +11,9 @@ def create_sample_file1(file): table.append(data[:4]) table.append(data[4:8]) table.append(data[8:12]) - pyexcel.save_as(array=table, dest_file_name=file) + pyexcel.save_as( + array=table, dest_file_name=file, library="pyexcel-libxlsxw" + ) class PyexcelHatWriterBase: @@ -26,7 +28,11 @@ class PyexcelHatWriterBase: } def test_series_table(self): - pyexcel.save_as(adict=self.content, dest_file_name=self.testfile) + pyexcel.save_as( + adict=self.content, + dest_file_name=self.testfile, + library="pyexcel-libxlsxw", + ) r = pyexcel.get_sheet(file_name=self.testfile, name_columns_by_row=0) eq_(r.dict, self.content) @@ -47,7 +53,9 @@ class PyexcelWriterBase: ] def _create_a_file(self, file): - pyexcel.save_as(dest_file_name=file, array=self.content) + pyexcel.save_as( + dest_file_name=file, array=self.content, library="pyexcel-libxlsxw" + ) def test_write_array(self): self._create_a_file(self.testfile) @@ -58,7 +66,11 @@ def test_write_array(self): class PyexcelMultipleSheetBase: def _write_test_file(self, filename): - pyexcel.save_book_as(bookdict=self.content, dest_file_name=filename) + pyexcel.save_book_as( + bookdict=self.content, + dest_file_name=filename, + library="pyexcel-libxlsxw", + ) def _clean_up(self): if os.path.exists(self.testfile2): diff --git a/tests/test_bug_fixes.py b/tests/test_bug_fixes.py index 307384e..a1f2f76 100644 --- a/tests/test_bug_fixes.py +++ b/tests/test_bug_fixes.py @@ -11,8 +11,13 @@ import pyexcel as pe from pyexcel_libxlsxw import save_data +from nose.tools import eq_ + class TestBugFix(TestCase): + def setUp(self): + self.maxDiff = None + def test_pyexcel_issue_5(self): """pyexcel issue #5 @@ -21,10 +26,10 @@ def test_pyexcel_issue_5(self): s = pe.load( os.path.join("tests", "test-fixtures", "test-date-format.xls") ) - s.save_as("issue5.xlsx") + s.save_as("issue5.xlsx", library="pyexcel-libxlsxw") s2 = pe.load("issue5.xlsx") - assert s[0, 0] == datetime.datetime(2015, 11, 11, 11, 12, 0) - assert s2[0, 0] == datetime.datetime(2015, 11, 11, 11, 12, 0) + eq_(s[0, 0], datetime.datetime(2015, 11, 11, 11, 12, 0)) + eq_(s2[0, 0], datetime.datetime(2015, 11, 11, 11, 12, 0)) def test_pyexcel_issue_8_with_physical_file(self): """pyexcel issue #8 @@ -33,7 +38,7 @@ def test_pyexcel_issue_8_with_physical_file(self): """ tmp_file = "issue_8_save_as.xlsx" s = pe.load(os.path.join("tests", "test-fixtures", "test8.xlsx")) - s.save_as(tmp_file) + s.save_as(tmp_file, library="pyexcel-libxlsxw") s2 = pe.load(tmp_file) self.assertEqual(str(s), str(s2)) content = dedent( @@ -58,7 +63,7 @@ def test_pyexcel_issue_8_with_memory_file(self): tmp_file = "issue_8_save_as.xlsx" f = open(os.path.join("tests", "test-fixtures", "test8.xlsx"), "rb") s = pe.load_from_memory("xlsx", f.read()) - s.save_as(tmp_file) + s.save_as(tmp_file, library="pyexcel-libxlsxw") s2 = pe.load(tmp_file) self.assertEqual(str(s), str(s2)) content = dedent( diff --git a/tests/test_formatters.py b/tests/test_formatters.py index ed039d7..69be3db 100644 --- a/tests/test_formatters.py +++ b/tests/test_formatters.py @@ -14,7 +14,11 @@ def test_writing_date_format(self): datetime.datetime(2014, 12, 25, 11, 11, 11), ] ] - pe.save_as(dest_file_name=excel_filename, array=data) + pe.save_as( + dest_file_name=excel_filename, + array=data, + library="pyexcel-libxlsxw", + ) r = pe.get_sheet(file_name=excel_filename, library="pyexcel-xls") assert isinstance(r[0, 0], datetime.date) is True assert r[0, 0].strftime("%d/%m/%y") == "25/12/14" diff --git a/tests/test_multiple_sheets.py b/tests/test_multiple_sheets.py index 8570043..7fde05b 100644 --- a/tests/test_multiple_sheets.py +++ b/tests/test_multiple_sheets.py @@ -33,7 +33,11 @@ def _write_test_file(self, file): 3,3,3,3 """ self.rows = 3 - pyexcel.save_book_as(bookdict=self.content, dest_file_name=file) + pyexcel.save_book_as( + bookdict=self.content, + dest_file_name=file, + library="pyexcel-libxlsxw", + ) def setUp(self): self.testfile = "multiple3.xlsx" @@ -219,17 +223,6 @@ def tearDown(self): os.unlink(self.testfile2) -class TestMultiSheetReader: - def setUp(self): - self.testfile = "file_with_an_empty_sheet.xlsx" - - def test_reader_with_correct_sheets(self): - r = pyexcel.BookReader( - os.path.join("tests", "fixtures", self.testfile) - ) - assert r.number_of_sheets() == 3 - - def _produce_ordered_dict(): data_dict = OrderedDict() data_dict.update({"Sheet1": [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]}) diff --git a/tests/test_stringio.py b/tests/test_stringio.py index 870b8bc..a5502fa 100644 --- a/tests/test_stringio.py +++ b/tests/test_stringio.py @@ -1,27 +1,15 @@ -import os - import pyexcel -from base import create_sample_file1 + from nose.tools import eq_ class TestStringIO: - def test_ods_stringio(self): - odsfile = "cute.xlsx" - create_sample_file1(odsfile) - with open(odsfile, "rb") as f: - content = f.read() - r = pyexcel.get_sheet(file_type="xlsx", file_content=content) - result = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", 1.1, 1] - actual = list(r.enumerate()) - eq_(result, actual) - if os.path.exists(odsfile): - os.unlink(odsfile) - def test_xls_output_stringio(self): data = [[1, 2, 3], [4, 5, 6]] - io = pyexcel.save_as(dest_file_type="xlsx", array=data) + io = pyexcel.save_as( + dest_file_type="xlsx", array=data, library="pyexcel-libxlsxw" + ) r = pyexcel.get_sheet(file_type="xlsx", file_content=io.getvalue()) result = [1, 2, 3, 4, 5, 6] actual = list(r.enumerate())