Skip to content

Commit bf0e271

Browse files
authored
New style writer (#7)
* 🎉 new style writer * This is an auto-commit, updating project meta data, such as changelog.rst, contributors.rst * Update rnd_requirements.txt * Update pyexcel-xlsxw.yml * This is an auto-commit, updating project meta data, such as changelog.rst, contributors.rst Co-authored-by: chfw <chfw@users.noreply.github.com>
1 parent b9bb33f commit bf0e271

File tree

12 files changed

+112
-86
lines changed

12 files changed

+112
-86
lines changed

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
copyright = '2015-2019 Onni Software Ltd.'
2626
author = 'chfw'
2727
# The short X.Y version
28-
version = '0.4.3'
28+
version = '0.6.0'
2929
# The full version, including alpha/beta/rc tags
3030
release = '0.4.3'
3131

pyexcel-xlsxw.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
overrides: "pyexcel.yaml"
22
name: "pyexcel-xlsxw"
33
nick_name: xlsxw
4-
version: 0.4.3
5-
current_version: 0.4.3
4+
version: 0.6.0
5+
current_version: 0.6.0
66
release: 0.4.3
77
file_type: xlsx
88
dependencies:
99
- XlsxWriter>=0.9.3
1010
- pyexcel-io>=0.4.0
11+
test_dependencies:
12+
- pyexcel
13+
- pyexcel-xls
14+
- pyexcel-xlsx
1115
description: A wrapper library to write data in xlsx and xlsm format

pyexcel_xlsxw/__init__.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,24 @@
77
:copyright: (c) 2016 by Onni Software Ltd & its contributors
88
:license: New BSD License
99
"""
10+
from pyexcel_io.io import get_data as read_data
11+
from pyexcel_io.io import isstream
12+
from pyexcel_io.io import store_data as write_data
13+
1014
# flake8: noqa
1115
# this line has to be place above all else
1216
# because of dynamic import
13-
from pyexcel_io.plugins import IOPluginInfoChain
14-
from pyexcel_io.io import get_data as read_data, isstream, store_data as write_data
15-
17+
from pyexcel_io.plugins import IOPluginInfoChainV2
1618

17-
__FILE_TYPE__ = 'xlsx'
18-
IOPluginInfoChain(__name__).add_a_writer(
19-
relative_plugin_class_path='xlsxw.XLSXWriter',
19+
__FILE_TYPE__ = "xlsx"
20+
IOPluginInfoChainV2(__name__).add_a_writer(
21+
relative_plugin_class_path="xlsxw.XLSXWriter",
22+
locations=["file"],
2023
file_types=[__FILE_TYPE__],
21-
stream_type='binary'
24+
stream_type="binary",
2225
)
2326

27+
2428
def save_data(afile, data, file_type=None, **keywords):
2529
"""standalone module function for writing module supported file type"""
2630
if isstream(afile) and file_type is None:

pyexcel_xlsxw/xlsxw.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
:license: New BSD License
99
"""
1010
import xlsxwriter
11+
from pyexcel_io.plugin_api.abstract_sheet import ISheetWriter
12+
from pyexcel_io.plugin_api.abstract_writer import IWriter
1113

12-
from pyexcel_io.book import BookWriter
13-
from pyexcel_io.sheet import SheetWriter
1414

15-
16-
class XLSXSheetWriter(SheetWriter):
15+
class XLSXSheetWriter(ISheetWriter):
1716
"""
1817
xlsx sheet writer
1918
"""
20-
def set_sheet_name(self, name):
19+
20+
def __init__(self, ods_book, ods_sheet, sheet_name, **_):
21+
self._native_book = ods_book
22+
self._native_sheet = ods_sheet
2123
self.current_row = 0
2224

2325
def write_row(self, array):
@@ -28,16 +30,23 @@ def write_row(self, array):
2830
self._native_sheet.write(self.current_row, i, array[i])
2931
self.current_row += 1
3032

33+
def close(self):
34+
pass
35+
3136

32-
class XLSXWriter(BookWriter):
37+
class XLSXWriter(IWriter):
3338
"""
3439
xlsx writer
3540
"""
36-
def __init__(self):
37-
BookWriter.__init__(self)
38-
self._native_book = None
3941

40-
def open(self, file_name, **keywords):
42+
def __init__(
43+
self,
44+
file_alike_object,
45+
file_type,
46+
constant_memory=True,
47+
default_date_format="dd/mm/yy",
48+
**keywords
49+
):
4150
"""
4251
Open a file for writing
4352
@@ -50,17 +59,16 @@ def open(self, file_name, **keywords):
5059
can be found in `xlsxwriter's documentation
5160
<http://xlsxwriter.readthedocs.io/workbook.html>`_
5261
"""
53-
keywords.setdefault('default_date_format', 'dd/mm/yy')
54-
keywords.setdefault('constant_memory', True)
55-
BookWriter.open(self, file_name, **keywords)
56-
62+
if "single_sheet_in_book" in keywords:
63+
keywords.pop("single_sheet_in_book")
5764
self._native_book = xlsxwriter.Workbook(
58-
file_name, keywords
59-
)
65+
file_alike_object, options=keywords
66+
)
6067

6168
def create_sheet(self, name):
62-
return XLSXSheetWriter(self._native_book,
63-
self._native_book.add_worksheet(name), name)
69+
return XLSXSheetWriter(
70+
self._native_book, self._native_book.add_worksheet(name), name
71+
)
6472

6573
def close(self):
6674
"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
NAME = "pyexcel-xlsxw"
3131
AUTHOR = "chfw"
32-
VERSION = "0.4.3"
32+
VERSION = "0.6.0"
3333
EMAIL = "info@pyexcel.org"
3434
LICENSE = "New BSD"
3535
DESCRIPTION = (

tests/base.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import os
2+
23
import pyexcel
34
from nose.tools import eq_
45

56

67
def create_sample_file1(file):
7-
data = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 1.1, 1]
8+
data = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", 1.1, 1]
89
table = []
910
table.append(data[:4])
1011
table.append(data[4:8])
@@ -16,10 +17,11 @@ class PyexcelHatWriterBase:
1617
"""
1718
Abstract functional test for hat writers
1819
"""
20+
1921
content = {
2022
"X": [1, 2, 3, 4, 5],
2123
"Y": [6, 7, 8, 9, 10],
22-
"Z": [11, 12, 13, 14, 15]
24+
"Z": [11, 12, 13, 14, 15],
2325
}
2426

2527
def test_series_table(self):
@@ -35,11 +37,12 @@ class PyexcelWriterBase:
3537
testfile and testfile2 have to be initialized before
3638
it is used for testing
3739
"""
40+
3841
content = [
3942
[1, 2, 3, 4, 5],
4043
[1, 2, 3, 4, 5],
4144
[1, 2, 3, 4, 5],
42-
[1, 2, 3, 4, 5]
45+
[1, 2, 3, 4, 5],
4346
]
4447

4548
def _create_a_file(self, file):
@@ -53,7 +56,6 @@ def test_write_array(self):
5356

5457

5558
class PyexcelMultipleSheetBase:
56-
5759
def _write_test_file(self, filename):
5860
pyexcel.save_book_as(bookdict=self.content, dest_file_name=filename)
5961

@@ -79,7 +81,7 @@ def test_reading_through_sheets(self):
7981
expected = [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]]
8082
assert data == expected
8183
data = list(b["Sheet3"].rows())
82-
expected = [[u'X', u'Y', u'Z'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]
84+
expected = [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]
8385
assert data == expected
8486
sheet3 = b["Sheet3"]
8587
sheet3.name_columns_by_row(0)

tests/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ collective.checkdocs
99
pygments
1010
moban
1111
moban_jinja2_github
12+
pyexcel
13+
pyexcel-xls
14+
pyexcel-xlsx

tests/test_bug_fixes.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33
This file keeps all fixes for issues found
44
55
"""
6-
import os
76
import datetime
7+
import os
88
from textwrap import dedent
99
from unittest import TestCase
10+
1011
import pyexcel as pe
1112

1213
from pyexcel_xlsxw import save_data
1314

1415

1516
class TestBugFix(TestCase):
16-
1717
def test_pyexcel_issue_5(self):
1818
"""pyexcel issue #5
1919
2020
datetime is not properly parsed
2121
"""
22-
s = pe.load(os.path.join("tests",
23-
"test-fixtures",
24-
"test-date-format.xls"))
22+
s = pe.load(
23+
os.path.join("tests", "test-fixtures", "test-date-format.xls")
24+
)
2525
s.save_as("issue5.xlsx")
2626
s2 = pe.load("issue5.xlsx")
2727
assert s[0, 0] == datetime.datetime(2015, 11, 11, 11, 12, 0)
@@ -33,21 +33,21 @@ def test_pyexcel_issue_8_with_physical_file(self):
3333
formular got lost
3434
"""
3535
tmp_file = "issue_8_save_as.xlsx"
36-
s = pe.load(os.path.join("tests",
37-
"test-fixtures",
38-
"test8.xlsx"))
36+
s = pe.load(os.path.join("tests", "test-fixtures", "test8.xlsx"))
3937
s.save_as(tmp_file)
4038
s2 = pe.load(tmp_file)
4139
self.assertEqual(str(s), str(s2))
42-
content = dedent("""
40+
content = dedent(
41+
"""
4342
CNY:
4443
+----------+----------+------+---+-------+
4544
| 01/09/13 | 02/09/13 | 1000 | 5 | 13.89 |
4645
+----------+----------+------+---+-------+
4746
| 02/09/13 | 03/09/13 | 2000 | 6 | 33.33 |
4847
+----------+----------+------+---+-------+
4948
| 03/09/13 | 04/09/13 | 3000 | 7 | 58.33 |
50-
+----------+----------+------+---+-------+""").strip("\n")
49+
+----------+----------+------+---+-------+"""
50+
).strip("\n")
5151
self.assertEqual(str(s2), content)
5252
os.unlink(tmp_file)
5353

@@ -57,23 +57,22 @@ def test_pyexcel_issue_8_with_memory_file(self):
5757
formular got lost
5858
"""
5959
tmp_file = "issue_8_save_as.xlsx"
60-
f = open(os.path.join("tests",
61-
"test-fixtures",
62-
"test8.xlsx"),
63-
"rb")
64-
s = pe.load_from_memory('xlsx', f.read())
60+
f = open(os.path.join("tests", "test-fixtures", "test8.xlsx"), "rb")
61+
s = pe.load_from_memory("xlsx", f.read())
6562
s.save_as(tmp_file)
6663
s2 = pe.load(tmp_file)
6764
self.assertEqual(str(s), str(s2))
68-
content = dedent("""
65+
content = dedent(
66+
"""
6967
CNY:
7068
+----------+----------+------+---+-------+
7169
| 01/09/13 | 02/09/13 | 1000 | 5 | 13.89 |
7270
+----------+----------+------+---+-------+
7371
| 02/09/13 | 03/09/13 | 2000 | 6 | 33.33 |
7472
+----------+----------+------+---+-------+
7573
| 03/09/13 | 04/09/13 | 3000 | 7 | 58.33 |
76-
+----------+----------+------+---+-------+""").strip("\n")
74+
+----------+----------+------+---+-------+"""
75+
).strip("\n")
7776
self.assertEqual(str(s2), content)
7877
os.unlink(tmp_file)
7978

@@ -86,8 +85,9 @@ def test_workbook_options(self):
8685
cell_content = "= Hello World ="
8786
tmp_file = "workbook_options.xlsx"
8887
data = {"Sheet 1": [[cell_content]]}
89-
save_data(tmp_file, data, strings_to_formulas=False,
90-
library='pyexcel-xlsxw')
88+
save_data(
89+
tmp_file, data, strings_to_formulas=False, library="pyexcel-xlsxw"
90+
)
9191
sheet = pe.get_sheet(file_name=tmp_file)
9292
self.assertEqual(sheet[0][0], cell_content)
9393
os.unlink(tmp_file)

tests/test_formatters.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
import pyexcel as pe
21
import datetime
32
import os
43

4+
import pyexcel as pe
5+
56

67
class TestDateFormat:
78
def test_writing_date_format(self):
89
excel_filename = "testdateformat.xlsx"
9-
data = [[datetime.date(2014, 12, 25),
10+
data = [
11+
[
12+
datetime.date(2014, 12, 25),
1013
datetime.time(11, 11, 11),
11-
datetime.datetime(2014, 12, 25, 11, 11, 11)]]
14+
datetime.datetime(2014, 12, 25, 11, 11, 11),
15+
]
16+
]
1217
pe.save_as(dest_file_name=excel_filename, array=data)
1318
r = pe.get_sheet(file_name=excel_filename, library="pyexcel-xls")
1419
assert isinstance(r[0, 0], datetime.date) is True

0 commit comments

Comments
 (0)