A flask extension using pyexcel to read, manipulate and write data in different excel formats: csv, ods, xls, xlsx and xlsm.

Overview

Flask-Excel - Let you focus on data, instead of file formats

https://raw.githubusercontent.com/pyexcel/pyexcel.github.io/master/images/patreon.png https://travis-ci.org/pyexcel-webwares/Flask-Excel.svg?branch=master https://pepy.tech/badge/Flask-Excel/month https://img.shields.io/static/v1?label=continuous%20templating&message=%E6%A8%A1%E7%89%88%E6%9B%B4%E6%96%B0&color=blue&style=flat-square https://img.shields.io/static/v1?label=coding%20style&message=black&color=black&style=flat-square https://readthedocs.org/projects/flask-excel/badge/?version=latest

Support the project

If your company has embedded pyexcel and its components into a revenue generating product, please support me on github, patreon or bounty source to maintain the project and develop it further.

If you are an individual, you are welcome to support me too and for however long you feel like. As my backer, you will receive early access to pyexcel related contents.

And your issues will get prioritized if you would like to become my patreon as pyexcel pro user.

With your financial support, I will be able to invest a little bit more time in coding, documentation and writing interesting posts.

Known constraints

Fonts, colors and charts are not supported.

Nor to read password protected xls, xlsx and ods files.

Introduction

Here is a typical conversation between the developer and the user:

User: "I have uploaded an excel file"
      "but your application says un-supported file format"
Developer: "Did you upload an xlsx file or a csv file?"
User: "Well, I am not sure. I saved the data using "
      "Microsoft Excel. Surely, it must be in an excel format."
Developer: "OK. Here is the thing. I were not told to support"
           "all available excel formats in day 1. Live with it"
           "or delay the project x number of days."

Flask-Excel is based on pyexcel and makes it easy to consume/produce information stored in excel files over HTTP protocol as well as on file system. This library can turn the excel data into a list of lists, a list of records(dictionaries), dictionaries of lists. And vice versa. Hence it lets you focus on data in Flask based web development, instead of file formats.

The idea originated from the common usability problem: when an excel file driven web application is delivered for non-developer users (ie: team assistant, human resource administrator etc). The fact is that not everyone knows (or cares) about the differences between various excel formats: csv, xls, xlsx are all the same to them. Instead of training those users about file formats, this library helps web developers to handle most of the excel file formats by providing a common programming interface. To add a specific excel file format type to you application, all you need is to install an extra pyexcel plugin. Hence no code changes to your application and no issues with excel file formats any more. Looking at the community, this library and its associated ones try to become a small and easy to install alternative to Pandas.

The highlighted features are:

  1. excel data import into and export from databases
  2. turn uploaded excel file directly into Python data structure
  3. pass Python data structures as an excel file download
  4. provide data persistence as an excel file in server side
  5. supports csv, tsv, csvz, tsvz by default and other formats are supported via the following plugins:
A list of file formats supported by external plugins
Package name Supported file formats Dependencies
pyexcel-io csv, csvz [1], tsv, tsvz [2]  
pyexcel-xls xls, xlsx(read only), xlsm(read only) xlrd, xlwt
pyexcel-xlsx xlsx openpyxl
pyexcel-ods3 ods pyexcel-ezodf, lxml
pyexcel-ods ods odfpy
Dedicated file reader and writers
Package name Supported file formats Dependencies
pyexcel-xlsxw xlsx(write only) XlsxWriter
pyexcel-libxlsxw xlsx(write only) libxlsxwriter
pyexcel-xlsxr xlsx(read only) lxml
pyexcel-xlsbr xlsb(read only) pyxlsb
pyexcel-odsr read only for ods, fods lxml
pyexcel-odsw write only for ods loxun
pyexcel-htmlr html(read only) lxml,html5lib
pyexcel-pdfr pdf(read only) camelot

Plugin shopping guide

Since 2020, all pyexcel-io plugins have dropped the support for python version lower than 3.6. If you want to use any python verions, please use pyexcel-io and its plugins version lower than 0.6.0.

Except csv files, xls, xlsx and ods files are a zip of a folder containing a lot of xml files

The dedicated readers for excel files can stream read

In order to manage the list of plugins installed, you need to use pip to add or remove a plugin. When you use virtualenv, you can have different plugins per virtual environment. In the situation where you have multiple plugins that does the same thing in your environment, you need to tell pyexcel which plugin to use per function call. For example, pyexcel-ods and pyexcel-odsr, and you want to get_array to use pyexcel-odsr. You need to append get_array(..., library='pyexcel-odsr').

Other data renderers
Package name Supported file formats Dependencies Python versions
pyexcel-text write only:rst, mediawiki, html, latex, grid, pipe, orgtbl, plain simple read only: ndjson r/w: json tabulate 2.6, 2.7, 3.3, 3.4 3.5, 3.6, pypy
pyexcel-handsontable handsontable in html handsontable same as above
pyexcel-pygal svg chart pygal 2.7, 3.3, 3.4, 3.5 3.6, pypy
pyexcel-sortable sortable table in html csvtotable same as above
pyexcel-gantt gantt chart in html frappe-gantt except pypy, same as above

Footnotes

[1] zipped csv file
[2] zipped tsv file

This library makes information processing involving various excel files as easy as processing array, dictionary when processing file upload/download, data import into and export from SQL databases, information analysis and persistence. It uses pyexcel and its plugins:

  1. to provide one uniform programming interface to handle csv, tsv, xls, xlsx, xlsm and ods formats.
  2. to provide one-stop utility to import the data in uploaded file into a database and to export tables in a database as excel files for file download.
  3. to provide the same interface for information persistence at server side: saving a uploaded excel file to and loading a saved excel file from file system.

Tested Flask Versions

Since 2020 Auguest, latest pyexcel-io version 0.6.0 forced Flask-Excel to use Python 3.6+

For older Flask versions and lower Python version, please use pyexcel-io<=0.5.20.

Installation

You can install Flask-Excel via pip:

$ pip install Flask-Excel

or clone it and install it:

$ git clone https://github.com/pyexcel-webwares/Flask-Excel.git
$ cd Flask-Excel
$ python setup.py install

Usage

Here are some example codes:

from flask import Flask, request, jsonify
import flask_excel

app=Flask(__name__)
flask_excel.init_excel(app)

@app.route("/upload", methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        return jsonify({"result": request.get_array(field_name='file')})
    return '''
    <!doctype html>
    <title>Upload an excel file</title>
    <h1>Excel file upload (csv, tsv, csvz, tsvz only)</h1>
    <form action="" method=post enctype=multipart/form-data>
    <p><input type=file name=file><input type=submit value=Upload>
   </form>
    '''

@app.route("/export", methods=['GET'])
def export_records():
    return excel.make_response_from_array([[1,2], [3, 4]], "csv",
                                          file_name="export_data")

if __name__ == "__main__":
    app.run()

Development guide

Development steps for code changes

  1. git clone https://github.com/pyexcel/Flask-Excel.git
  2. cd Flask-Excel

Upgrade your setup tools and pip. They are needed for development and testing only:

  1. pip install --upgrade setuptools pip

Then install relevant development requirements:

  1. pip install -r rnd_requirements.txt # if such a file exists
  2. pip install -r requirements.txt
  3. pip install -r tests/requirements.txt

Once you have finished your changes, please provide test case(s), relevant documentation and update CHANGELOG.rst.

Note

As to rnd_requirements.txt, usually, it is created when a dependent library is not released. Once the dependecy is installed (will be released), the future version of the dependency in the requirements.txt will be valid.

How to test your contribution

Although nose and doctest are both used in code testing, it is adviable that unit tests are put in tests. doctest is incorporated only to make sure the code examples in documentation remain valid across different development releases.

On Linux/Unix systems, please launch your tests like this:

$ make

On Windows systems, please issue this command:

> test.bat

Before you commit

Please run:

$ make format

so as to beautify your code otherwise travis-ci may fail your unit test.

License

New BSD License

Comments
  • No suitable database adapter found! pyexcel/Flask-Excel

    No suitable database adapter found! pyexcel/Flask-Excel

    We have a file, we have created the tables with the same column names and everything seems to be fine. We get the error: No suitable database adapter found! When we try to upload any excel file.

    When we try to upload any excel file. We have tried this with a basic excel file mimicing the same columns in case there was an issue with the originating file. This has not resolved the issue. We have also created a new file with the same column names and entered some basic data and this still returns the same error.

    opened by AnastasiaLd 17
  • Interoperability with uWSGI?

    Interoperability with uWSGI?

    I'm filing this issue to see if others have successfully used this package with uWSGI as I ran into some issues using this package with uWSGI. Specifically, excel.make_response_from_array(....) would return None. Responses were fine when using the default flask development server.

    It's entirely possible that this is due to some server configuration issues on my end. Sadly I didn't have much time to look into the issue as the associated feature development is on a deadline and since flask excel was just exporting a csv, I removed the package entirely and went with a raw python implementation.

    opened by Jrokisky 8
  • How to ignore blank lines with flask-excel when import from xlsx?

    How to ignore blank lines with flask-excel when import from xlsx?

    I used flask-excel to import data from xlsx file. When blank lines exist in xlsx file, pyexcel gives '' for empty cell and inserts '' in every row into database. When I have a row unique, code will raise error.

    Is there a simple way to ignore blank lines? Thanks.

    question 
    opened by yaoelvon 8
  • TypeError: invalid file: None

    TypeError: invalid file: None

    The example from the Quick Start works perfectly but when I try to use the get_dict() or get_array() functions when responding to requests made from Flask-Admin, I get the following error:

    Here is the traceback: https://gist.github.com/af22b27762543a62709ea89897bc0539 .

    I call it from here:

    @pre_save(sender=BulkMessage)
    def on_bulk_message_save_handler(model_class, instance, created):
        instance.file_fields = request.get_array(field_name='file')
    

    The request object does have the correct file name at the point of calling get_array():

    dump(request.files)
    Contents of werkzeug.datastructures.ImmutableMultiDict object at 0x7fc8e6daf3b8
    file    
    
    <FileStorage: 'SMS_dummy_data.xlsx' ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')>
    

    But the file contents are lost somewhere in get_array():

    File "/home/tuk/.virtualenvs/bulksms/lib/python3.4/site-packages/pyexcel_webio/__init__.py", line 93, in get_dict
    
    return pe.get_dict(**params)
    
    [console ready]
    >>> dump()
    Local variables in frame
    params  
    
    {'file_content': b'', 'name_columns_by_row': 0, 'file_type': 'xlsx'}
    
    keywords    
    
    {'name_columns_by_row': True, 'field_name': 'file'}
    
    self    
    
    <ExcelRequest 'http://localhost:5000/admin/bulkmessage/new/?url=%2Fadmin%2Fbulkmessage%2F' [POST]>
    

    Any ideas?

    opened by tiktuk 7
  • the response of make_response_from_array(array,file_type,file_name) is None! Different project ,same array, but different response.One is None. One is <Response 87649 bytes [200 OK]>.WHY?

    the response of make_response_from_array(array,file_type,file_name) is None! Different project ,same array, but different response.One is None. One is .WHY?

    The response of make_response_from_array(array,file_type,file_name) is None! Different project ,same array, same logic, but different response. One is None. One is <Response 87649 bytes [200 OK]>. Could you please tell what happened ? and what's wrong with my project? I don't know how to describe this question!!! Hope you can see it. Thanks you very much!!!

    opened by hannah168 5
  • make_response_from_records with large data sets (xlsx) crashes

    make_response_from_records with large data sets (xlsx) crashes

    If I call make_response_from_records() to convert to CSV with 1M rows, it does it no problem. However if I do the same thing to XLSX, it runs memory up until the whole flask application comes crashing down and dies.

    Obviously an Excel spreadsheet can't normally have 1M rows in it anyhow.

    I can check the number of rows before I call make_response_from_records() to mitigate this problem.

    I was opening this issue to see if that function could be updated to throw an exception if too many rows are passed instead of happily ingesting them until it dies.

    opened by rotten 5
  • adding file_name option to make_response

    adding file_name option to make_response

    I wanted a convenient way to provide the file name to be presented in the content-disposition header instead of passing it in via a form.

    This overrides the proxied pyexcel make* methods and adds an extra (default none) file_name option. This could also be accomplished with a decorator, either in the client's code or within init.py, but I decided this was clearer.

    Only the name of the file, not the extension, needs to be provided. The extension will be added based on the file_type argument passed in.

    opened by abehrens 4
  • make_response_from_records returns deprecated warning

    make_response_from_records returns deprecated warning

    When make_response_from_records() is invoked it displays a warning:

    "Deprecated since v0.1.5! Please use get_sheet instead."

    I'm using Flask-Excel (0.0.3) with pyexcel (0.1.7).

    It looks like it is coming out of code here: https://github.com/chfw/pyexcel/blob/master/pyexcel/deprecated.py

    opened by rotten 4
  •  Sheet: Category does not match any given tables.Please be aware of case sensitivity.

    Sheet: Category does not match any given tables.Please be aware of case sensitivity.

    It is a question. It's the example from docs I copy the code and download the sample-data. It works but when I rename the sheet in the sample file, like changing category into Category, It complains like this.

    Sheet: Category does not match any given tables.Please be aware of case sensitivity.

    And I check the code and manual carefully and still don't know how this "match" works. Without understanding this I can't use save_book_to_database. Could you explain how it works?

    Thanks, and sorry for my poor English.

    opened by Rorshachk 3
  • How can I used make_response_from_query_sets() where column_names change to new name

    How can I used make_response_from_query_sets() where column_names change to new name

    column_names = ['chapter_id','story_id', 'story_url']
    return excel.make_response_from_query_sets(story_list, column_names, file_name='test',file_type='xlsx')
    

    I want used column_names = ['chapterid','storyid', 'storyurl'] replace and my query is outerjoin ,so How I can make it to excel

    opened by zengaorong 3
  • Only in wsgi environment, occur response type error

    Only in wsgi environment, occur response type error

    Hi,

    [My environment] Windows 10 Python 3.6 IIS 7.5 or Apache 2.4.37 (I checked both environment with fastcgi, mod_wsgi)

    [Condition] Below code run well in flask cmd mode(ex> python excel_test.py) http://localhost:5000/export But when run in IIS or Apache meet TypeError(maybe 'None' Returned).

    [Sample Code] @app.route("/export", methods=['GET']) def export_records(): return excel.make_response_from_array([[1, 2], [3, 4]], "xlsx", file_name="export_data")

    [Error] Error occurred: Traceback (most recent call last): File "c:\python\lib\site-packages\flask\app.py", line 2292, in wsgi_app response = self.full_dispatch_request() File "c:\python\lib\site-packages\flask\app.py", line 1816, in full_dispatch_request return self.finalize_request(rv) File "c:\python\lib\site-packages\flask\app.py", line 1831, in finalize_request response = self.make_response(rv) File "c:\python\lib\site-packages\flask\app.py", line 1957, in make_response 'The view function did not return a valid response. The' TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

    [Workaround] I had googling and find this code well worked in IIS, Apache.

    @app.route("/export2", methods=['GET']) def export_records2(): csvlist = [[1, 2], [3, 4]] df = pd.DataFrame(csvlist, columns=["Team", "Player"])

    print(df)
    
    output = io.BytesIO()
    writer = pd.ExcelWriter(output)
    df.to_excel(writer, 'Tab1')
    writer.save()
    
    resp = make_response(output.getvalue())
    resp.headers['Content-Disposition'] = 'attachment; filename=output.xlsx'
    resp.headers["Content-type"] = "text/csv"
    return resp
    

    Thanks for your help

    opened by maifire 3
  • SQLAlchemy's Enum-Type unsupported

    SQLAlchemy's Enum-Type unsupported

    Flask-Excel doesn't support sqlalchemy's Enum type.

    Example:

    import enum
    
    class Animal(enum.Enum):
        dog = "Dog"
        cat = "Cat"
        rabbit = "Rabbit"
    
    class Child(db.Model):
        name = db.Column(db.String(100))
        age = db.Column(db.Integer)
        animal = db.Column(db.Enum(Animal))
    
    opened by ikreb7 0
  • AttributeError  for quickstart sample

    AttributeError for quickstart sample

    Flask 1.1.2 Flask-Excel 0.0.7

    when running the quick start sample from http://flask.pyexcel.org/en/latest/, it raised error:

    • http://localhost:5000/download AttributeError: module 'flask_excel' has no attribute 'make_response_from_array'

    • http://localhost:5000/upload AttributeError: 'Request' object has no attribute 'get_array'

    opened by forestlzj 0
  • Export Template

    Export Template

    Does this package support exporting HTML tables (jinja template) to excel? It would be a nice feature to have. Sometimes we might just want to design the table layout and it is easy to design in HTML.

    opened by ghost 1
  • Multiple Sheet using Flask Excel

    Multiple Sheet using Flask Excel

    I was wondering what the best way is for creating a multiple sheet Excel book. I have data that is being generated live (nothing from SQL).

    My idea was to create a pyexcel_instance, use 'make_response()'. However, I was slightly confused with the best way to use PyExcel. Should I save my Pyexcel book when I make one? If so, would I have to delete the saved object to avoid memory leaks?

    opened by varunsampat30 1
  • IndexError: Index out of range

    IndexError: Index out of range

    Hi, when i use the save_book_to_database almost keeping with your example i kept getting the Index out of range error, but with other functions such as get_dict it works fine, what could be the problem?

    opened by juandiegovd 2
Releases(v0.0.7)
A Flask extension that enables or disables features based on configuration.

Flask FeatureFlags This is a Flask extension that adds feature flagging to your applications. This lets you turn parts of your site on or off based on

Rachel Greenfield 131 Sep 26, 2022
HTTP security headers for Flask

Talisman: HTTP security headers for Flask Talisman is a small Flask extension that handles setting HTTP headers that can help protect against a few co

Google Cloud Platform 853 Dec 19, 2022
Curso Desenvolvimento avançado Python com Flask e REST API

Curso Desenvolvimento avançado Python com Flask e REST API Curso da Digital Innovation One Professor: Rafael Galleani Conteudo do curso Introdução ao

Elizeu Barbosa Abreu 1 Nov 14, 2021
SQL Alchemy dialect for Neo4j

SQL Alchemy dialect for Neo4j This package provides the SQL dialect for Neo4j, using the official JDBC driver (the Neo4j "BI Connector" ) Installation

Beni Ben zikry 8 Jan 02, 2023
Flask-app scaffold, generate flask restful backend

Flask-app scaffold, generate flask restful backend

jacksmile 1 Nov 24, 2021
Map Matching & Weight Completion service - Java (Springboot) & Python(Flask)

Map Matching service to match coordinates to roads using Java and Springboot. Weight Completion service to fill in missing weights in a graph, using Python and Flask.

2 May 13, 2022
Neo4j Movies Example application with Flask backend using the neo4j-python-driver

Neo4j Movies Application: Quick Start This example application demonstrates how easy it is to get started with Neo4j in Python. It is a very simple we

Neo4j Examples 309 Dec 24, 2022
A web application for a fake pizza store, built in Python with Flask and PostgreSQL.

✨ Pizza Pizza - Pizza Store ✨ A web application for a fake Pizza Store, the app let you create an account and order pizza, complements or drinks. Buil

Bonnie Fave 6 Dec 18, 2022
Flask-Bcrypt is a Flask extension that provides bcrypt hashing utilities for your application.

Flask-Bcrypt Flask-Bcrypt is a Flask extension that provides bcrypt hashing utilities for your application. Due to the recent increased prevelance of

Max Countryman 310 Dec 14, 2022
Sample Dockerized flask app deployed on Kubernetes on Azure using AKS

Sample Dockerized flask app deployed on Kubernetes on Azure using AKS

Ahmed khémiri 22 Sep 08, 2021
Rich implementation for Flask

Flask Rich Implements the Rich programming library with Flask. All features are toggleable, including: Better logging Colorful tracebacks Usage Import

BD103 13 Jun 06, 2022
A caching extension for Flask

Flask-Caching Adds easy cache support to Flask. This is a fork of the Flask-Cache extension. Flask-Caching also includes the cache module from werkzeu

Peter Justin 774 Jan 02, 2023
docker-compose uWSGI nginx flask

docker-compose uWSGI nginx flask Note that this was tested on CentOS 7 Usage sudo yum install docker

Abdolkarim Saeedi 3 Sep 11, 2022
Easy file uploads for Flask.

Library that works with Flask & SqlAlchemy to store files on your server & in your database Read the docs: Documentation Installation Please install t

Joe Gasewicz 145 Jan 06, 2023
Pagination support for flask

flask-paginate Pagination support for flask framework (study from will_paginate). It supports several css frameworks. It requires Python2.6+ as string

Lix Xu 264 Nov 07, 2022
Connect is a Python Flask project within the cloud-native ecosystem

Connect is a Python Flask project within the cloud-native ecosystem. Second project of Udacity's Cloud Native Nanodegree program, focusing on documenting and architecting a monolith migration to micr

Lauren Ferreira 3 Feb 28, 2022
Full-Stack application that visualizes amusement park safety.

Amusement Park Ride Safety Analysis Project Proposal We have chosen to look into amusement park data to explore ride safety relationships visually, in

Michael Absher 0 Jul 11, 2021
Small flask based opds catalog designed to serve a directory via OPDS

teenyopds Small flask based opds catalog designed to serve a directory via OPDS, it has currently only been verified to work with KyBook 3 on iOS but

Adam Furbee 4 Jul 14, 2022
Another redis monitor by using flask, angular, socket.io

RedisPAPA we use redis info to monitor the redis usage. PAPA means a father who is monitoring the redis. accoding to the redis doc, it is be recommand

no13bus 393 Dec 30, 2022
Full Stack Web Development with Flask.

Discover Flask Full Stack Web Development with Flask. http://discoverflask.com Flask is a micro web framework powered by Python. Its API is fairly sma

Real Python 4.4k Jan 06, 2023