I looked through the code and there's a Format API for creating custom parsers. The drawback of this is that there is nothing about that class in the docs. So we could contribute to this project by adding the docs and examples.
I've made some research about this but it's not yet tested:
class XLS(TablibFormat):
TABLIB_MODULE = 'tablib.formats._xls'
CONTENT_TYPE = 'application/vnd.ms-excel'
def create_dataset(self, in_stream):
"""
Create dataset from first sheet.
"""
import xlrd
xls_book = xlrd.open_workbook(file_contents=in_stream)
dataset = tablib.Dataset()
sheet = xls_book.sheets()[0]
dataset.headers = sheet.row_values(0)
for i in range(1, sheet.nrows):
dataset.append(sheet.row_values(i))
return dataset
TABLIB_MODULE - we don't actually want it because we will be using our own parser instead of tablib, and also we will use Format instead of TablibFormat
create_dataset - this function takes in_stream which is the content of the imported file. This function returns data in form of tablib.Dataset. So the task sounds simple -> convert our parser, so it will return substitutions in this format.
http://docs.python-tablib.org/en/master/tutorial/#creating-a-dataset
The headers of the dataset should match the field names.
For that, we can use this: https://github.com/django-import-export/django-import-export
I looked through the code and there's a
Format
API for creating custom parsers. The drawback of this is that there is nothing about that class in the docs. So we could contribute to this project by adding the docs and examples.I've made some research about this but it's not yet tested:
Format
Example Format (copied from https://github.com/django-import-export/django-import-export/blob/master/import_export/formats/base_formats.py)
TABLIB_MODULE
- we don't actually want it because we will be using our own parser instead of tablib, and also we will useFormat
instead ofTablibFormat
create_dataset
- this function takesin_stream
which is the content of the imported file. This function returns data in form oftablib.Dataset
. So the task sounds simple -> convert our parser, so it will return substitutions in this format. http://docs.python-tablib.org/en/master/tutorial/#creating-a-dataset The headers of the dataset should match the field names.