1+ import pyexcel as pe
2+ import sys
3+ PY2 = sys .version_info [0 ] == 2
4+ if PY2 :
5+ from StringIO import StringIO as BytesIO
6+ else :
7+ from io import BytesIO
8+
9+
10+ FILE_TYPE_MIME_TABLE = {
11+ "csv" : "text/csv" ,
12+ "tsv" : "text/tab-separated-values" ,
13+ "csvz" : "application/zip" ,
14+ "tsvz" : "application/zip" ,
15+ "ods" : "application/vnd.oasis.opendocument.spreadsheet" ,
16+ "xls" : "application/vnd.ms-excel" ,
17+ "xlsx" : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ,
18+ "xlsm" : "application/vnd.ms-excel.sheet.macroenabled.12"
19+ }
20+
21+
22+ class ExcelInput (object ):
23+ """A generic interface for an excel file to be converted
24+
25+ The source could be from anywhere, memory or file system
26+ """
27+ def load_single_sheet (self , file_name , sheet_name = None , ** keywords ):
28+ """Abstract method
29+
30+ :param form_field_name: the file field name in the html form for file upload
31+ :param sheet_name: For an excel book, there could be multiple sheets. If it is left
32+ unspecified, the sheet at index 0 is loaded. For 'csv', 'tsv' file,
33+ *sheet_name* should be None anyway.
34+ :param keywords: additional key words
35+ :returns: A sheet object
36+ """
37+ raise NotImplementedError ("Please implement this function" )
38+
39+ def load_book (self , file_name , ** keywords ):
40+ """Abstract method
41+
42+ :param form_field_name: the file field name in the html form for file upload
43+ :param keywords: additional key words
44+ :returns: A instance of :class:`Book`
45+ """
46+ raise NotImplementedError ("Please implement this function" )
47+
48+ def get_sheet (self , file_name , sheet_name = None , ** keywords ):
49+ """
50+ Get a :class:`Sheet` instance from the file
51+
52+ :param form_field_name: the file field name in the html form for file upload
53+ :param sheet_name: For an excel book, there could be multiple sheets. If it is left
54+ unspecified, the sheet at index 0 is loaded. For 'csv', 'tsv' file,
55+ *sheet_name* should be None anyway.
56+ :param keywords: additional key words
57+ :returns: A sheet object
58+ """
59+ return self .load_single_sheet (file_name , sheet_name , ** keywords )
60+
61+ def get_array (self , file_name , sheet_name = None , ** keywords ):
62+ """
63+ Get a list of lists from the file
64+
65+ :param form_field_name: the file field name in the html form for file upload
66+ :param sheet_name: For an excel book, there could be multiple sheets. If it is left
67+ unspecified, the sheet at index 0 is loaded. For 'csv', 'tsv' file,
68+ *sheet_name* should be None anyway.
69+ :param keywords: additional key words
70+ :returns: A list of lists
71+ """
72+ sheet = self .get_sheet (file_name , sheet_name , ** keywords )
73+ return sheet .to_array ()
74+
75+ def get_dict (self , file_name , sheet_name = None , name_columns_by_row = 0 , name_rows_by_column = - 1 , ** keywords ):
76+ """Get a dictionary from the file
77+
78+ :param form_field_name: the file field name in the html form for file upload
79+ :param sheet_name: For an excel book, there could be multiple sheets. If it is left
80+ unspecified, the sheet at index 0 is loaded. For 'csv', 'tsv' file,
81+ *sheet_name* should be None anyway.
82+ :param keywords: additional key words
83+ :returns: A dictionary
84+ """
85+ sheet = self .load_single_sheet (file_name , sheet_name ,
86+ name_columns_by_row = name_columns_by_row ,
87+ name_rows_by_column = name_rows_by_column , ** keywords )
88+ return sheet .to_dict ()
89+
90+ def get_records (self , file_name , sheet_name = None , name_columns_by_row = 0 , name_rows_by_column = - 1 , ** keywords ):
91+ """Get a list of records from the file
92+
93+ :param form_field_name: the file field name in the html form for file upload
94+ :param sheet_name: For an excel book, there could be multiple sheets. If it is left
95+ unspecified, the sheet at index 0 is loaded. For 'csv', 'tsv' file,
96+ *sheet_name* should be None anyway.
97+ :param keywords: additional key words
98+ :returns: A list of records
99+ """
100+ sheet = self .load_single_sheet (file_name , sheet_name ,
101+ name_columns_by_row = name_columns_by_row ,
102+ name_rows_by_column = name_rows_by_column , ** keywords )
103+ return sheet .to_records ()
104+
105+ def get_book (self , file_name , ** keywords ):
106+ """Get a instance of :class:`Book` from the file
107+ :param form_field_name: the file field name in the html form for file upload
108+ :param keywords: additional key words
109+ :returns: A instance of :class:`Book`
110+ """
111+ return self .load_book (file_name , ** keywords )
112+
113+ def get_book_dict (self , file_name , ** keywords ):
114+ """Get a dictionary of two dimensional array from the file
115+
116+ :param form_field_name: the file field name in the html form for file upload
117+ :param keywords: additional key words
118+ :returns: A dictionary of two dimensional arrays
119+ """
120+ book = self .get_book (file_name , ** keywords )
121+ return book .to_dict ()
122+
123+ def dumpy_func (content , content_type = None , status = 200 ):
124+ return None
125+
126+ ExcelResponse = dumpy_func
127+
128+ def make_response (pyexcel_instance , file_type , status = 200 ):
129+ io = BytesIO ()
130+ pyexcel_instance .save_to_memory (file_type , io )
131+ io .seek (0 )
132+ return ExcelResponse (io .read (), content_type = FILE_TYPE_MIME_TABLE [file_type ], status = status )
133+
134+
135+ def make_response_from_array (array , file_type , status = 200 ):
136+ return make_response (pe .Sheet (array ), file_type , status )
137+
138+
139+ def make_response_from_dict (adict , file_type , status = 200 ):
140+ return make_response (pe .load_from_dict (adict ), file_type , status )
141+
142+
143+ def make_response_from_records (records , file_type , status = 200 ):
144+ return make_response (pe .load_from_records (records ), file_type , status )
145+
146+
147+ def make_response_from_book_dict (adict , file_type , status = 200 ):
148+ return make_response (pe .Book (adict ), file_type , status )
0 commit comments