Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
copyright = '2015-2019 Onni Software Ltd.'
author = 'chfw'
# The short X.Y version
version = '0.4.3'
version = '0.6.0'
# The full version, including alpha/beta/rc tags
release = '0.4.3'

Expand Down
8 changes: 6 additions & 2 deletions pyexcel-xlsxw.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
overrides: "pyexcel.yaml"
name: "pyexcel-xlsxw"
nick_name: xlsxw
version: 0.4.3
current_version: 0.4.3
version: 0.6.0
current_version: 0.6.0
release: 0.4.3
file_type: xlsx
dependencies:
- XlsxWriter>=0.9.3
- pyexcel-io>=0.4.0
test_dependencies:
- pyexcel
- pyexcel-xls
- pyexcel-xlsx
description: A wrapper library to write data in xlsx and xlsm format
18 changes: 11 additions & 7 deletions pyexcel_xlsxw/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@
:copyright: (c) 2016 by Onni Software Ltd & its contributors
:license: New BSD License
"""
from pyexcel_io.io import get_data as read_data
from pyexcel_io.io import isstream
from pyexcel_io.io import store_data as write_data

# flake8: noqa
# this line has to be place above all else
# because of dynamic import
from pyexcel_io.plugins import IOPluginInfoChain
from pyexcel_io.io import get_data as read_data, isstream, store_data as write_data

from pyexcel_io.plugins import IOPluginInfoChainV2

__FILE_TYPE__ = 'xlsx'
IOPluginInfoChain(__name__).add_a_writer(
relative_plugin_class_path='xlsxw.XLSXWriter',
__FILE_TYPE__ = "xlsx"
IOPluginInfoChainV2(__name__).add_a_writer(
relative_plugin_class_path="xlsxw.XLSXWriter",
locations=["file"],
file_types=[__FILE_TYPE__],
stream_type='binary'
stream_type="binary",
)


def save_data(afile, data, file_type=None, **keywords):
"""standalone module function for writing module supported file type"""
if isstream(afile) and file_type is None:
Expand Down
44 changes: 26 additions & 18 deletions pyexcel_xlsxw/xlsxw.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
:license: New BSD License
"""
import xlsxwriter
from pyexcel_io.plugin_api.abstract_sheet import ISheetWriter
from pyexcel_io.plugin_api.abstract_writer import IWriter

from pyexcel_io.book import BookWriter
from pyexcel_io.sheet import SheetWriter


class XLSXSheetWriter(SheetWriter):
class XLSXSheetWriter(ISheetWriter):
"""
xlsx sheet writer
"""
def set_sheet_name(self, name):

def __init__(self, ods_book, ods_sheet, sheet_name, **_):
self._native_book = ods_book
self._native_sheet = ods_sheet
self.current_row = 0

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

def close(self):
pass


class XLSXWriter(BookWriter):
class XLSXWriter(IWriter):
"""
xlsx writer
"""
def __init__(self):
BookWriter.__init__(self)
self._native_book = None

def open(self, file_name, **keywords):
def __init__(
self,
file_alike_object,
file_type,
constant_memory=True,
default_date_format="dd/mm/yy",
**keywords
):
"""
Open a file for writing

Expand All @@ -50,17 +59,16 @@ def open(self, file_name, **keywords):
can be found in `xlsxwriter's documentation
<http://xlsxwriter.readthedocs.io/workbook.html>`_
"""
keywords.setdefault('default_date_format', 'dd/mm/yy')
keywords.setdefault('constant_memory', True)
BookWriter.open(self, file_name, **keywords)

if "single_sheet_in_book" in keywords:
keywords.pop("single_sheet_in_book")
self._native_book = xlsxwriter.Workbook(
file_name, keywords
)
file_alike_object, options=keywords
)

def create_sheet(self, name):
return XLSXSheetWriter(self._native_book,
self._native_book.add_worksheet(name), name)
return XLSXSheetWriter(
self._native_book, self._native_book.add_worksheet(name), name
)

def close(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

NAME = "pyexcel-xlsxw"
AUTHOR = "chfw"
VERSION = "0.4.3"
VERSION = "0.6.0"
EMAIL = "info@pyexcel.org"
LICENSE = "New BSD"
DESCRIPTION = (
Expand Down
12 changes: 7 additions & 5 deletions tests/base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os

import pyexcel
from nose.tools import eq_


def create_sample_file1(file):
data = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 1.1, 1]
data = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", 1.1, 1]
table = []
table.append(data[:4])
table.append(data[4:8])
Expand All @@ -16,10 +17,11 @@ class PyexcelHatWriterBase:
"""
Abstract functional test for hat writers
"""

content = {
"X": [1, 2, 3, 4, 5],
"Y": [6, 7, 8, 9, 10],
"Z": [11, 12, 13, 14, 15]
"Z": [11, 12, 13, 14, 15],
}

def test_series_table(self):
Expand All @@ -35,11 +37,12 @@ class PyexcelWriterBase:
testfile and testfile2 have to be initialized before
it is used for testing
"""

content = [
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5],
]

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


class PyexcelMultipleSheetBase:

def _write_test_file(self, filename):
pyexcel.save_book_as(bookdict=self.content, dest_file_name=filename)

Expand All @@ -79,7 +81,7 @@ def test_reading_through_sheets(self):
expected = [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]]
assert data == expected
data = list(b["Sheet3"].rows())
expected = [[u'X', u'Y', u'Z'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]
expected = [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]
assert data == expected
sheet3 = b["Sheet3"]
sheet3.name_columns_by_row(0)
Expand Down
3 changes: 3 additions & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ collective.checkdocs
pygments
moban
moban_jinja2_github
pyexcel
pyexcel-xls
pyexcel-xlsx
38 changes: 19 additions & 19 deletions tests/test_bug_fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
This file keeps all fixes for issues found

"""
import os
import datetime
import os
from textwrap import dedent
from unittest import TestCase

import pyexcel as pe

from pyexcel_xlsxw import save_data


class TestBugFix(TestCase):

def test_pyexcel_issue_5(self):
"""pyexcel issue #5

datetime is not properly parsed
"""
s = pe.load(os.path.join("tests",
"test-fixtures",
"test-date-format.xls"))
s = pe.load(
os.path.join("tests", "test-fixtures", "test-date-format.xls")
)
s.save_as("issue5.xlsx")
s2 = pe.load("issue5.xlsx")
assert s[0, 0] == datetime.datetime(2015, 11, 11, 11, 12, 0)
Expand All @@ -33,21 +33,21 @@ def test_pyexcel_issue_8_with_physical_file(self):
formular got lost
"""
tmp_file = "issue_8_save_as.xlsx"
s = pe.load(os.path.join("tests",
"test-fixtures",
"test8.xlsx"))
s = pe.load(os.path.join("tests", "test-fixtures", "test8.xlsx"))
s.save_as(tmp_file)
s2 = pe.load(tmp_file)
self.assertEqual(str(s), str(s2))
content = dedent("""
content = dedent(
"""
CNY:
+----------+----------+------+---+-------+
| 01/09/13 | 02/09/13 | 1000 | 5 | 13.89 |
+----------+----------+------+---+-------+
| 02/09/13 | 03/09/13 | 2000 | 6 | 33.33 |
+----------+----------+------+---+-------+
| 03/09/13 | 04/09/13 | 3000 | 7 | 58.33 |
+----------+----------+------+---+-------+""").strip("\n")
+----------+----------+------+---+-------+"""
).strip("\n")
self.assertEqual(str(s2), content)
os.unlink(tmp_file)

Expand All @@ -57,23 +57,22 @@ def test_pyexcel_issue_8_with_memory_file(self):
formular got lost
"""
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())
f = open(os.path.join("tests", "test-fixtures", "test8.xlsx"), "rb")
s = pe.load_from_memory("xlsx", f.read())
s.save_as(tmp_file)
s2 = pe.load(tmp_file)
self.assertEqual(str(s), str(s2))
content = dedent("""
content = dedent(
"""
CNY:
+----------+----------+------+---+-------+
| 01/09/13 | 02/09/13 | 1000 | 5 | 13.89 |
+----------+----------+------+---+-------+
| 02/09/13 | 03/09/13 | 2000 | 6 | 33.33 |
+----------+----------+------+---+-------+
| 03/09/13 | 04/09/13 | 3000 | 7 | 58.33 |
+----------+----------+------+---+-------+""").strip("\n")
+----------+----------+------+---+-------+"""
).strip("\n")
self.assertEqual(str(s2), content)
os.unlink(tmp_file)

Expand All @@ -86,8 +85,9 @@ def test_workbook_options(self):
cell_content = "= Hello World ="
tmp_file = "workbook_options.xlsx"
data = {"Sheet 1": [[cell_content]]}
save_data(tmp_file, data, strings_to_formulas=False,
library='pyexcel-xlsxw')
save_data(
tmp_file, data, strings_to_formulas=False, library="pyexcel-xlsxw"
)
sheet = pe.get_sheet(file_name=tmp_file)
self.assertEqual(sheet[0][0], cell_content)
os.unlink(tmp_file)
11 changes: 8 additions & 3 deletions tests/test_formatters.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import pyexcel as pe
import datetime
import os

import pyexcel as pe


class TestDateFormat:
def test_writing_date_format(self):
excel_filename = "testdateformat.xlsx"
data = [[datetime.date(2014, 12, 25),
data = [
[
datetime.date(2014, 12, 25),
datetime.time(11, 11, 11),
datetime.datetime(2014, 12, 25, 11, 11, 11)]]
datetime.datetime(2014, 12, 25, 11, 11, 11),
]
]
pe.save_as(dest_file_name=excel_filename, array=data)
r = pe.get_sheet(file_name=excel_filename, library="pyexcel-xls")
assert isinstance(r[0, 0], datetime.date) is True
Expand Down
Loading