Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read xlsx file uploaded in django?

I have a django server which allow user to upload xlsx files, I want to access the data in it.

I know about openpyxl, however, it looks like it doesn't have a way to parse opened files, I do not want to save the file on disk and read it again.

How can I do it?

like image 873
dspjm Avatar asked Dec 10 '22 16:12

dspjm


1 Answers

Actually, it's simpler than above:

    from openpyxl import load_workbook
    ...
    if request.method == 'POST':
        form = UploadForm(request.POST, request.FILES)
        if form.is_valid():
            wb = load_workbook(filename=request.FILES['file'].file)

The file attribute of FILES['file'] already returns a BytesIO instance ;)

like image 138
Ricardo Avatar answered Dec 24 '22 06:12

Ricardo