For web developer
The following libraries are written to facilitate the daily import and export of excel data.
framework |
plugin/middleware/extension |
|---|---|
Flask |
|
Django |
|
Pyramid |
And you may make your own by using pyexcel-webio
Read any supported excel and respond its content in json
You can find a real world example in examples/memoryfile/ directory: pyexcel_server.py. Here is the example snippet
1def upload():
2 if request.method == 'POST' and 'excel' in request.files:
3 # handle file upload
4 filename = request.files['excel'].filename
5 extension = filename.split(".")[-1]
6 # Obtain the file extension and content
7 # pass a tuple instead of a file name
8 content = request.files['excel'].read()
9 if sys.version_info[0] > 2:
10 # in order to support python 3
11 # have to decode bytes to str
12 content = content.decode('utf-8')
13 sheet = pe.get_sheet(file_type=extension, file_content=content)
14 # then use it as usual
15 sheet.name_columns_by_row(0)
16 # respond with a json
17 return jsonify({"result": sheet.dict})
18 return render_template('upload.html')
request.files[‘excel’] in line 4 holds the file object. line 5 finds out the file extension. line 13 obtains a sheet instance. line 15 uses the first row as data header. line 17 sends the json representation of the excel file back to client browser.
Write to memory and respond to download
1data = [
2 [...],
3 ...
4]
5
6@app.route('/download')
7def download():
8 sheet = pe.Sheet(data)
9 output = make_response(sheet.csv)
10 output.headers["Content-Disposition"] = "attachment; filename=export.csv"
11 output.headers["Content-type"] = "text/csv"
12 return output
make_response is a Flask utility to make a memory content as http response.
Note
You can find the corresponding source code at examples/memoryfile