Ejemplo n.º 1
0
def export(data, fields, sheet_title=''):
    """
    @param data: Dictionary of {key: value} pairs, or list of dictionaries
    @param fields: The keys of the dictionaries that we want to export

    Serializes the data into an excel file, and returns the sheet
    """
    # if ``data`` is a dictionary, we make it into a list
    if isinstance(data, dict):
        data = [data, ]

    # We layout the values into a list of tuples, with each tuple element
    # corresponding to a header
    values = []
    for element in data:
        values.append(
            [_clean(element.get(key)) for key in fields]
        )

    # Create list of headers, to make it mutable for removing style tags below
    headers = list(fields)

    # Detect formatting based on header values. Currently supported:
    #   **bold**
    columns_format = {}
    for i, head in enumerate(headers):
        if head.startswith('**') and head.endswith('**'):
            style = xlwt.Style.XFStyle()
            style.font.bold = True
            columns_format[i] = style
            headers[i] = head[2:-2]

    # In headers: replace underscores with spaces and capitalize them
    headers = [header.replace('_', ' ').capitalize() for header in headers]

    # Create the tablib dataset
    dataset = Dataset(*values, headers=headers, title=sheet_title)

    # Create worksheet from dataset
    workbook = xlwt.Workbook(encoding='utf8')
    worksheet = workbook.add_sheet(dataset.title, cell_overwrite_ok=True)
    xls.dset_sheet(dataset, worksheet)  # actual conversion

    # Overwrite columns with formatted cells
    for col, style in columns_format.iteritems():
        for row in range(dataset.height):
            # ``row+1``: pass over the header row, which is present in worksheet
            worksheet.write(row+1, col, dataset[row][col], style)

    # Export to excel
    stream = BytesIO()
    workbook.save(stream)
    stream.seek(0)

    return stream
Ejemplo n.º 2
0
def export_set(dataset):
    """Returns XLS representation of Dataset."""

    wb = xlwt.Workbook(encoding='utf8')
    ws = wb.add_sheet(dataset.title if dataset.title else 'Tablib Dataset')

    dset_sheet(dataset, ws)

    stream = BytesIO()
    wb.save(stream)
    return stream.getvalue()
Ejemplo n.º 3
0
def export_set(dataset):
    """Returns XLS representation of Dataset."""

    wb = xlwt.Workbook(encoding='utf8')
    ws = wb.add_sheet(dataset.title if dataset.title else 'Tablib Dataset')

    dset_sheet(dataset, ws)

    stream = BytesIO()
    wb.save(stream)
    return stream.getvalue()
Ejemplo n.º 4
0
def export_book(databook):
    """Returns XLS representation of DataBook."""

    wb = xlwt.Workbook(encoding='utf8')

    for i, dset in enumerate(databook._datasets):
        ws = wb.add_sheet(dset.title if dset.title else 'Sheet%s' % (i))

        dset_sheet(dset, ws)

    stream = BytesIO()
    wb.save(stream)
    return stream.getvalue()
Ejemplo n.º 5
0
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)
    return stream.getvalue()
Ejemplo n.º 6
0
Archivo: _ods.py Proyecto: ZoeGeng/Art
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)
    return stream.getvalue()
Ejemplo n.º 7
0
def export_book(databook):
    """Returns XLS representation of DataBook."""

    wb = xlwt.Workbook(encoding='utf8')

    for i, dset in enumerate(databook._datasets):
        ws = wb.add_sheet(dset.title if dset.title else 'Sheet%s' % (i))

        dset_sheet(dset, ws)


    stream = BytesIO()
    wb.save(stream)
    return stream.getvalue()
Ejemplo n.º 8
0
    def write(self, dataset):
        f = BytesIO()
        stream = StringIO()

        for row in dataset._package():
            for key, val in row.items():
                stream.write('-' * len(key) + '\n')
                stream.write(key.encode('utf-8') + '\n')
                stream.write('-' * len(key) + '\n')
                stream.write(val.encode('utf-8') + '\n\n')
            stream.write('\n' + '*' * 50 + '\n\n\n')

        f.write(stream.getvalue())
        return f
Ejemplo n.º 9
0
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)
    return stream.getvalue()
Ejemplo n.º 10
0
Archivo: _ods.py Proyecto: ZoeGeng/Art
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)
    return stream.getvalue()
Ejemplo n.º 11
0
 def write(self, dataset):
     data = self.format.export_set(dataset)
     f = BytesIO()
     f.write(data)
     return f