diff options
| author | Mark Rogers <f4nt@f4ntasmic.com> | 2011-05-12 15:59:57 -0500 |
|---|---|---|
| committer | Mark Rogers <f4nt@f4ntasmic.com> | 2011-05-12 15:59:57 -0500 |
| commit | 5350355fbe0aefe053d40fda03c0688a7b7eae3d (patch) | |
| tree | 00d1f403dabdb54174890fbc0ba2b47f4fa9d8ac | |
| parent | 87ce64d4c859efb7c3d37b1e715fa04a0c1cc631 (diff) | |
| download | tablib-5350355fbe0aefe053d40fda03c0688a7b7eae3d.tar.gz | |
a bunch of cleanup from my previous commit
| -rw-r--r-- | AUTHORS | 3 | ||||
| -rw-r--r-- | setup.py | 1 | ||||
| -rw-r--r-- | tablib/core.py | 9 | ||||
| -rw-r--r-- | tablib/core25.py | 13 | ||||
| -rw-r--r-- | tablib/formats/_xlsx.py | 51 | ||||
| -rwxr-xr-x | test_tablib.py | 2 |
6 files changed, 49 insertions, 30 deletions
@@ -14,4 +14,5 @@ Patches and Suggestions - Josh Ourisman - Luca Beltrame - Benjamin Wohlwend -- Erik Youngren
\ No newline at end of file +- Erik Youngren +- Mark Rogers
\ No newline at end of file @@ -33,6 +33,7 @@ setup( 'tablib', 'tablib.formats', 'tablib.packages', 'tablib.packages.xlwt', + 'tablib.packages.openpyxl', 'tablib.packages.yaml', 'tablib.packages.unicodecsv' ], diff --git a/tablib/core.py b/tablib/core.py index 71f4f71..348b88f 100644 --- a/tablib/core.py +++ b/tablib/core.py @@ -392,6 +392,15 @@ class Dataset(object): @property def xlsx(): + """An Excel Spreadsheet representation of the :class:`Dataset` object, with :ref:`separators`. Cannot be set. + + .. admonition:: Binary Warning + + :class:`Dataset.xlsx` contains binary data, so make sure to write in binary mode:: + + with open('output.xlsx', 'wb') as f: + f.write(data.xlsx)' + """ pass diff --git a/tablib/core25.py b/tablib/core25.py index c8352a6..a48fbb9 100644 --- a/tablib/core25.py +++ b/tablib/core25.py @@ -392,6 +392,19 @@ class Dataset(object): """ pass + @property + def xlsx(): + """An Excel Spreadsheet representation of the :class:`Dataset` object, with :ref:`separators`. Cannot be set. + + .. admonition:: Binary Warning + + :class:`Dataset.xlsx` contains binary data, so make sure to write in binary mode:: + + with open('output.xlsx', 'wb') as f: + f.write(data.xlsx)' + """ + pass + @property def csv(): diff --git a/tablib/formats/_xlsx.py b/tablib/formats/_xlsx.py index 92e33f9..1a0713b 100644 --- a/tablib/formats/_xlsx.py +++ b/tablib/formats/_xlsx.py @@ -7,30 +7,20 @@ import sys if sys.version_info[0] > 2: - from io import BytesIO - import tablib.packages.xlwt3 as xlwt - + from io import BytesIO else: from cStringIO import StringIO as BytesIO - import tablib.packages.xlwt as xlwt from tablib.packages.openpyxl.workbook import Workbook from tablib.packages.openpyxl.writer.excel import ExcelWriter from tablib.packages.openpyxl.cell import get_column_letter - - title = 'xlsx' extentions = ('xlsx',) -# special styles -#wrap = xlwt.easyxf("alignment: wrap on") -#bold = xlwt.easyxf("font: bold on") - - def export_set(dataset): - """Returns XLS representation of Dataset.""" + """Returns XLSX representation of Dataset.""" wb = Workbook() ws = wb.worksheets[0] @@ -44,19 +34,19 @@ def export_set(dataset): def export_book(databook): - """Returns XLS representation of DataBook.""" + """Returns XLSX representation of DataBook.""" wb = Workbook() ew = ExcelWriter(workbook = wb) for i, dset in enumerate(databook._datasets): - ws = wb.add_sheet() + ws = wb.create_sheet() ws.title = dset.title if dset.title else 'Sheet%s' % (i) dset_sheet(dset, ws) stream = BytesIO() - ew.save(filename='test.xlsx') + ew.save(stream) return stream.getvalue() @@ -75,30 +65,33 @@ def dset_sheet(dataset, ws): # bold headers if (row_number == 1) and dataset.headers: - ws.cell('%s%s'%(col_idx, row_number)).value = '%s' % col - #ws.write(i, j, col, bold) - - # frozen header row - #ws.panes_frozen = True - #ws.horz_split_pos = 1 + ws.cell('%s%s'%(col_idx, row_number)).value = unicode( + '%s' % col, errors='ignore') + style = ws.get_style('%s%s' % (col_idx, row_number)) + style.font.bold = True + ws.freeze_panes = '%s%s' % (col_idx, row_number) # bold separators elif len(row) < dataset.width: - ws.cell('%s%s'%(col_idx, row_number)).value = '%s' % col - #ws.write(i, j, col, bold) + ws.cell('%s%s'%(col_idx, row_number)).value = unicode( + '%s' % col, errors='ignore') + style = ws.get_style('%s%s' % (col_idx, row_number)) + style.font.bold = True # wrap the rest else: try: if '\n' in col: - ws.cell('%s%s'%(col_idx, row_number)).value = '%s' % col - #ws.write(i, j, col, wrap) + ws.cell('%s%s'%(col_idx, row_number)).value = unicode( + '%s' % col, errors='ignore') + style = ws.get_style('%s%s' % (col_idx, row_number)) + style.alignment.wrap_text else: - ws.cell('%s%s'%(col_idx, row_number)).value = '%s' % col - #ws.write(i, j, col) + ws.cell('%s%s'%(col_idx, row_number)).value = unicode( + '%s' % col, errors='ignore') except TypeError: - ws.cell('%s%s'%(col_idx, row_number)).value = '%s' % col - #ws.write(i, j, col) + ws.cell('%s%s'%(col_idx, row_number)).value = unicode( + '%s' % col, errors='ignore') diff --git a/test_tablib.py b/test_tablib.py index 7791935..4346859 100755 --- a/test_tablib.py +++ b/test_tablib.py @@ -222,6 +222,7 @@ class TablibTestCase(unittest.TestCase): data.csv data.tsv data.xls + data.xlsx def test_book_export_no_exceptions(self): @@ -233,6 +234,7 @@ class TablibTestCase(unittest.TestCase): book.json book.yaml book.xls + book.xlsx def test_json_import_set(self): |
