pyexcel-webio

https://api.travis-ci.org/chfw/pyexcel-webio.png https://coveralls.io/repos/chfw/pyexcel-webio/badge.png?branch=master

pyexcel-webio is a tiny interface library to unify the web extensions that uses pyexcel . You may use it to write a web extension for your faviourite Python web framework.

Installation

You can install it via pip:

$ pip install pyexcel-webio

or clone it and install it:

$ git clone http://github.com/chfw/pyexcel-webio.git
$ cd pyexcel-webio
$ python setup.py install

Known extensions

framework plugin/middleware/extension
Flask Flask-Excel
Django django-excel
Pyramid pyramid-excel

Usage

This small section outlines the steps to adapt pyexcel-webio for your favourite web framework. For illustration purpose, I took Flask micro-framework as an example.

  1. Inherit ExcelInput class and implement load_single_sheet and load_book methods depending on the parameters you will have. For example, Flask.Request put the incoming file in Flask.Request.files and the key is the field name in the html form:

    from flask import Flask, Request
    import pyexcel as pe
    from pyexcel.ext import webio
    
    class ExcelRequest(webio.ExcelInput, Request):
        def _get_file_tuple(self, field_name):
            filehandle = self.files[field_name]
            filename = filehandle.filename
            extension = filename.split(".")[1]
            return extension, filehandle
    
        def load_single_sheet(self, field_name=None, sheet_name=None,
                              **keywords):
            file_type, file_handle = self._get_file_tuple(field_name)
            return pe.get_sheet(file_type=file_type,
                                content=file_handle.read(),
                                sheet_name=sheet_name,
                                **keywords)
    
        def load_book(self, field_name=None, **keywords):
            file_type, file_handle = self._get_file_tuple(field_name)
            return pe.get_book(file_type=file_type,
                               content=file_handle.read(),
                               **keywords)
    
  2. Plugin in a response method that has the following signature:

    def your_func(content, content_type=None, status=200):
        ....
    

    or a response class has the same signature:

    class YourClass:
        def __init__(self, content, content_type=None, status=200):
        ....
    

    For example, with Flask, it is just a few lines:

    from flask import Response
    
    
    webio.ExcelResponse = Response
    
  3. Then make the proxy for make_response series by simply copying the following lines to your extension:

    from pyexcel.ext.webio import (
        make_response,
        make_response_from_array,
        make_response_from_dict,
        make_response_from_records,
        make_response_from_book_dict
    )
    

License

New BSD License

Dependencies

  • pyexcel >= 0.1.7

API Reference

pyexcel.ext.webio

A generic request and response interface for pyexcel web extensions

copyright:
  1. 2015 by Onni Software Ltd.
license:

New BSD License

Excel file upload

Here are the api for processing excel file upload

class pyexcel_webio.ExcelInput

A generic interface for an excel file input

The source could be from anywhere, memory or file system

get_array(sheet_name=None, **keywords)

Get a list of lists from the file

Parameters:
  • sheet_name – For an excel book, there could be multiple sheets. If it is left unspecified, the sheet at index 0 is loaded. For ‘csv’, ‘tsv’ file, sheet_name should be None anyway.
  • keywords – additional key words
Returns:

A list of lists

get_book(**keywords)

Get a instance of Book from the file

Parameters:keywords – additional key words
Returns:A instance of Book
get_book_dict(**keywords)

Get a dictionary of two dimensional array from the file

Parameters:keywords – additional key words
Returns:A dictionary of two dimensional arrays
get_dict(sheet_name=None, name_columns_by_row=0, **keywords)

Get a dictionary from the file

Parameters:
  • sheet_name – For an excel book, there could be multiple sheets. If it is left unspecified, the sheet at index 0 is loaded. For ‘csv’, ‘tsv’ file, sheet_name should be None anyway.
  • keywords – additional key words
Returns:

A dictionary

get_records(sheet_name=None, name_columns_by_row=0, **keywords)

Get a list of records from the file

Parameters:
  • sheet_name – For an excel book, there could be multiple sheets. If it is left unspecified, the sheet at index 0 is loaded. For ‘csv’, ‘tsv’ file, sheet_name should be None anyway.
  • keywords – additional key words
Returns:

A list of records

get_sheet(sheet_name=None, **keywords)

Get a Sheet instance from the file

Parameters:
  • sheet_name – For an excel book, there could be multiple sheets. If it is left unspecified, the sheet at index 0 is loaded. For ‘csv’, ‘tsv’ file, sheet_name should be None anyway.
  • keywords – additional key words
Returns:

A sheet object

load_book(**keywords)

Abstract method

Parameters:
  • form_field_name – the file field name in the html form for file upload
  • keywords – additional key words
Returns:

A instance of Book

load_single_sheet(sheet_name=None, **keywords)

Abstract method

Parameters:
  • sheet_name – For an excel book, there could be multiple sheets. If it is left unspecified, the sheet at index 0 is loaded. For ‘csv’, ‘tsv’ file, sheet_name should be None anyway.
  • keywords – additional key words
Returns:

A sheet object

save_book_to_database(session=None, tables=None, initializers=None, mapdicts=None, auto_commit=True, **keywords)

Save a book into database

Parameters:
  • session – a SQLAlchemy session
  • tables – a list of database tables
  • initializers – a list of model initialization functions.
  • mapdicts – a list of explicit table column names if your excel data sheets do not have the exact column names
  • keywords – additional keywords to pyexcel.Book.save_to_database()
save_to_database(session=None, table=None, initializer=None, mapdict=None, auto_commit=True, sheet_name=None, name_columns_by_row=0, name_rows_by_column=-1, field_name=None, **keywords)

Save data from a sheet to database

Parameters:
  • session – a SQLAlchemy session
  • table – a database table
  • initializer – a custom table initialization function if you have one
  • mapdict – the explicit table column names if your excel data do not have the exact column names
  • keywords – additional keywords to pyexcel.Sheet.save_to_database()
class pyexcel_webio.ExcelInputInMultiDict

A generic interface for an upload excel file appearing in a dictionary

get_file_tuple(field_name)

Abstract method to get the file tuple

It is expected to return file type and a file handle to the uploaded file

load_book(field_name=None, **keywords)

Load the book from named form field

load_single_sheet(field_name=None, sheet_name=None, **keywords)

Load the single sheet from named form field

Excel file download

Here are the api for converted different data structure into a excel file download.

pyexcel_webio.make_response(pyexcel_instance, file_type, status=200, **keywords)

Make a http response from a pyexcel instance of Sheet or Book

Parameters:
  • pyexcel_instance – pyexcel.Sheet or pyexcel.Book
  • file_type

    one of the following strings:

    • ‘csv’
    • ‘tsv’
    • ‘csvz’
    • ‘tsvz’
    • ‘xls’
    • ‘xlsx’
    • ‘xlsm’
    • ‘ods’
  • status – unless a different status is to be returned.
Returns:

http response

pyexcel_webio.make_response_from_array(array, file_type, status=200, **keywords)

Make a http response from an array

Parameters:
Returns:

http response

pyexcel_webio.make_response_from_dict(adict, file_type, status=200, **keywords)

Make a http response from a dictionary of lists

Parameters:
Returns:

http response

pyexcel_webio.make_response_from_records(records, file_type, status=200, **keywords)

Make a http response from a list of dictionaries

Parameters:
Returns:

http response

pyexcel_webio.make_response_from_book_dict(adict, file_type, status=200, **keywords)

Make a http response from a dictionary of two dimensional arrays

Parameters:
Returns:

http response

pyexcel_webio.make_response_from_query_sets(query_sets, column_names, file_type, status=200, **keywords)

Make a http response from a dictionary of two dimensional arrays

Parameters:
  • query_sets – a query set
  • column_names – a nominated column names. It could not be N one, otherwise no data is returned.
  • file_type – same as make_response()
  • status – same as make_response()
Returns:

a http response

pyexcel_webio.make_response_from_a_table(session, table, file_type, status=200, **keywords)

Make a http response from sqlalchmey table

Parameters:
Returns:

a http response

pyexcel_webio.make_response_from_tables(session, tables, file_type, status=200, **keywords)

Make a http response from sqlalchmy tables

Parameters:
Returns:

a http response

Indices and tables

Table Of Contents

This Page