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.
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
framework | plugin/middleware/extension |
---|---|
Flask | Flask-Excel |
Django | django-excel |
Pyramid | pyramid-excel |
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.
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)
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
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
)
New BSD License
A generic request and response interface for pyexcel web extensions
copyright: |
|
---|---|
license: | New BSD License |
Here are the api for processing excel file upload
A generic interface for an excel file input
The source could be from anywhere, memory or file system
Get a list of lists from the file
Parameters: |
|
---|---|
Returns: | A list of lists |
Get a instance of Book from the file
Parameters: | keywords – additional key words |
---|---|
Returns: | A instance of Book |
Get a dictionary of two dimensional array from the file
Parameters: | keywords – additional key words |
---|---|
Returns: | A dictionary of two dimensional arrays |
Get a dictionary from the file
Parameters: |
|
---|---|
Returns: | A dictionary |
Get a list of records from the file
Parameters: |
|
---|---|
Returns: | A list of records |
Get a Sheet instance from the file
Parameters: |
|
---|---|
Returns: | A sheet object |
Abstract method
Parameters: |
|
---|---|
Returns: | A instance of Book |
Abstract method
Parameters: |
|
---|---|
Returns: | A sheet object |
Save a book into database
Parameters: |
|
---|
Save data from a sheet to database
Parameters: |
|
---|
A generic interface for an upload excel file appearing in a dictionary
Abstract method to get the file tuple
It is expected to return file type and a file handle to the uploaded file
Load the book from named form field
Load the single sheet from named form field
Here are the api for converted different data structure into a excel file download.
Make a http response from a pyexcel instance of Sheet or Book
Parameters: |
|
---|---|
Returns: | http response |
Make a http response from an array
Parameters: |
|
---|---|
Returns: | http response |
Make a http response from a dictionary of lists
Parameters: |
|
---|---|
Returns: | http response |
Make a http response from a list of dictionaries
Parameters: |
|
---|---|
Returns: | http response |
Make a http response from a dictionary of two dimensional arrays
Parameters: |
|
---|---|
Returns: | http response |
Make a http response from a dictionary of two dimensional arrays
Parameters: |
|
---|---|
Returns: | a http response |
Make a http response from sqlalchmey table
Parameters: |
|
---|---|
Returns: | a http response |
Make a http response from sqlalchmy tables
Parameters: |
|
---|---|
Returns: | a http response |