Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions flask_excel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
:license: New BSD License
"""
from flask import Flask, Request, Response
import pyexcel as pe
import pyexcel_webio as webio


Expand All @@ -17,10 +18,43 @@ class ExcelRequest(webio.ExcelInputInMultiDict, Request):
Mix in pyexcel's webio function signatures to Flask request
"""
def get_file_tuple(self, field_name):
filehandle = self.files[field_name]
filename = filehandle.filename
extension = filename.split(".")[1]
return extension, filehandle
# will upload multi files with the same filename
for filehandle in self.files.getlist(field_name):
filename = filehandle.filename
extension = filename.split(".")[1]
yield extension, filehandle

def get_array(self, **keywords):
"""
Get a list of lists from the file

:param 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.
:param keywords: additional key words
:returns: A list of lists
"""
result = []
for params in self.get_params(**keywords):
result.extend(pe.get_array(**params))
return result

def get_params(self, field_name=None, **keywords):
"""
Load the single sheet from named form field
"""
for file_type, file_handle in self.get_file_tuple(field_name):
if file_type is not None and file_handle is not None:
file_content = file_handle.read()
file_content = file_content.decode("gbk").encode("utf-8")
keywords = {
'file_type': file_type,
'file_content': file_content
}
yield keywords
else:
raise Exception("Invalid parameters")


# Plug-in the custom request to Flask
Expand Down