Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite ODS support based on loxun XMLWriter module #244

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
82 changes: 25 additions & 57 deletions tablib/formats/_ods.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,91 +3,59 @@
""" Tablib - ODF Support.
"""

from odf import opendocument, style, table, text
from tablib.packages import ods
from tablib.compat import BytesIO, unicode

title = 'ods'
extensions = ('ods',)

bold = style.Style(name="bold", family="paragraph")
bold.addElement(style.TextProperties(fontweight="bold", fontweightasian="bold", fontweightcomplex="bold"))

def export_set(dataset):
"""Returns ODF representation of Dataset."""

wb = opendocument.OpenDocumentSpreadsheet()
wb.automaticstyles.addElement(bold)

ws = table.Table(name=dataset.title if dataset.title else 'Tablib Dataset')
wb.spreadsheet.addElement(ws)
dset_sheet(dataset, ws)

stream = BytesIO()
wb.save(stream)
wb = ods.ODSWorkbook(stream)
dset_sheet(dataset, wb, title='Sheet')
wb.close()
return stream.getvalue()


def export_book(databook):
"""Returns ODF representation of DataBook."""

wb = opendocument.OpenDocumentSpreadsheet()
wb.automaticstyles.addElement(bold)

for i, dset in enumerate(databook._datasets):
ws = table.Table(name=dset.title if dset.title else 'Sheet%s' % (i))
wb.spreadsheet.addElement(ws)
dset_sheet(dset, ws)


stream = BytesIO()
wb.save(stream)
wb = ods.ODSWorkbook(stream)

for i, dataset in enumerate(databook._datasets):
dset_sheet(dataset, wb, title='Sheet %d' % i)
wb.close()
return stream.getvalue()


def dset_sheet(dataset, ws):
def dset_sheet(dataset, wb, title=None):
"""Completes given worksheet from given Dataset."""
_package = dataset._package(dicts=False)

if not _package:
return

wb.start_sheet(len(_package[0]),
dataset.title if dataset.title else title)
for i, sep in enumerate(dataset._separators):
_offset = i
_package.insert((sep[0] + _offset), (sep[1],))

for i, row in enumerate(_package):
row_number = i + 1
odf_row = table.TableRow(stylename=bold, defaultcellstylename='bold')
for j, col in enumerate(row):
cells = []
for cell in row:
try:
col = unicode(col, errors='ignore')
cell = unicode(cell, errors='ignore')
except TypeError:
## col is already unicode
# col is already unicode
pass
ws.addElement(table.TableColumn())

# bold headers
if (row_number == 1) and dataset.headers:
odf_row.setAttribute('stylename', bold)
ws.addElement(odf_row)
cell = table.TableCell()
p = text.P()
p.addElement(text.Span(text=col, stylename=bold))
cell.addElement(p)
odf_row.addElement(cell)

# wrap the rest
else:
try:
if '\n' in col:
ws.addElement(odf_row)
cell = table.TableCell()
cell.addElement(text.P(text=col))
odf_row.addElement(cell)
else:
ws.addElement(odf_row)
cell = table.TableCell()
cell.addElement(text.P(text=col))
odf_row.addElement(cell)
except TypeError:
ws.addElement(odf_row)
cell = table.TableCell()
cell.addElement(text.P(text=col))
odf_row.addElement(cell)
cells.append(cell)
if i == 0:
wb.add_headers(cells)
else:
wb.add_row(cells)
wb.end_sheet()
Loading