user5594877
user5594877

Reputation:

Django To upload and read and write large excel file

I am new to Django and i need my app to allow users to upload excel files. On server side I am reading the excel file by each cell, append some values and then translate the values and again write back to excel file and download the attachment. I am able to perform this action for small files, but for large file it gives me timeout error. Please see the below .

enter code here

def translatedoc(request):
    data=""
    convrowstr=""
    if request.method=='POST':
        response = StreamingHttpResponse (content_type='application/vnd.ms-excel')

        try:
            form=fileUpload(request.POST,request.FILES)
            if form.is_valid():
                input_file=request.FILES.get('file')
                sl=request.POST.get('fsl')
                if sl=="Detect Language":
                    sl="auto"
                else:
                    # get sl code from database
                    sl=languagecode.objects.filter(Language=sl).values_list('code')
                    sl=str(sl[0][0])
            # get tl code from database
                tl=languagecode.objects.filter(Language=request.POST.get('ftl')).values_list('code')
                wb = xlrd.open_workbook(file_contents=input_file.read())
                wb_sheet=wb.sheet_by_index(0)
                for rownum in range(0, wb_sheet.nrows):
                    convstr=""
                    for colnum in range(0,wb_sheet.ncols):
                        try:
                            rw=wb_sheet.cell_value(rownum,colnum)
                            if type(rw)==float or type(rw)==int:
                               convstr=convstr +'<td>' + str(rw)
                            else:
                                convstr=convstr +'<td>' + rw
                        except Exception as e:
                            pass                 
                    if len(convstr) + len(convrowstr) >20000:
                        # translate if the length of doc exceed the limit
                        #call google api module
                        data=data + translate(convrowstr,sl,str(tl[0][0]))
                        convrowstr=""
                    if rownum==wb_sheet.nrows-1:
                        convrowstr= convrowstr + "<tr>" + convstr
                        # translate for first or last
                        #call google api module
                        data=data + translate(convrowstr,sl,str(tl[0][0]))
                        convrowstr=""                       
                    convrowstr= convrowstr + "<tr>" + convstr 
                    log.error(rownum)
            if len(data)>1:
                sio=StringIO.StringIO()
                try:
                    workbook = xlwt.Workbook()
                    sheet = workbook.add_sheet("output")                  
                    row=0
                    for rw in data.split("<tr>")[1:]:
                        col=0
                        for cl in rw.split("<td>")[1:]:
                            try:
                                sheet.write(row,col,cl.split("<b>")[1].split("</b>")[0])
                            except Exception as e:
                                pass
                            col+=1
                        row+=1
                    workbook.save(sio)
                    sio.seek(0)
                    sv=sio.getvalue()

                    response['Content-Disposition'] = 'attachment; filename=Output.xls'
                    return response
                except Exception as e:
                    log.error(e)

        except Exception as e:
            log.error(e)

Upvotes: 2

Views: 865

Answers (1)

aman kumar
aman kumar

Reputation: 3156

you can do the through celery for large file upload. You can read the file in celery.

Upvotes: 1

Related Questions