4
votes

I am using django-import-export library to import several excel books. However, I have over 1,000 books that need to be imported into the db. Is there a way to select a folder to upload instead of selecting and uploading each individual file? I've worked through the tutorial found here: https://django-import-export.readthedocs.org/en/latest/getting_started.html#admin-integration but I was unable to find the answer to my question.

Any help would be greatly appreciated.

1

1 Answers

1
votes

Posting mainly for future viewers. Currently, django_import_export imports only the active/first sheet of a single excel workbook. However, the code is easy enough to modify and alleviate this problem. In forms.py, there is ImportForm which is the one used while importing from admin. Simply change the import_file field to something like this:

import_file = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': 
                             True}),
                             label=_('File to import')
                             )

This form is used in admin.py to process the file data. Change the linked line to something like:

import_files = request.FILES.getlist('import_file')
for import_file in import_files:
    ...

Now all that's left is to modify the import procedure in base_formats.py for XLS and XLSX formats. The changes will be nearly same for both, I will outline the XLS one here.

Instead of taking the first sheet, run a for loop over the sheets and append the data to the dataset.

dataset = tablib.Dataset()
first_sheet = True    # If you keep all correct headers only in first sheet
for sheet in xls_book.sheets():
    if first_sheet:
        dataset.headers = sheet.row_values(0)
    first_sheet = False
    for i in moves.range(1, sheet.nrows):
        dataset.append(sheet.row_values(i))
return dataset

For XLSX, the loop will run on xlsx_book.worksheets. Rest is similar to xls.

This will allow you to select multiple excel workbooks and import all the sheets for a workbook. I know the ideal solution would be to import a zip file to create all the data using a single bulk_create, but this serves well for now.