title: How to return a excel file back to a user using Django and Pandas
body: <p>I am currently taking an excel file from the user on my home.html page, passing it to my python, using the values to do some logic, and wanting to returned a excel file back to the user after the logic has edited the data. I had this working as an .exe on my computer already but wanted to move it to a web page. I am not currently getting any errors. Does anyone know how to do this, or know of a walkthrough that might can help me?</p>
  <pre><code>def page_objects(request):
      Area = request.POST[&quot;Area&quot;]
      Output = request.POST[&quot;Output&quot;]
      FilePath = request.FILES['myfile']
      if Output == 'List':
          zone = pd.read_excel(FilePath, sheet_name= &quot;Form&quot;, header=0, usecols=list(range(7)))
          WC = zone.iat[3,4]
          data = pd.read_excel(FilePath, sheet_name=&quot;Form&quot;, header=0, usecols=list(range(7)), skiprows=8) 
          data2= pd.read_excel(FilePath, sheet_name=&quot;Form&quot;, header=0, usecols=list(range(7)), skiprows=8)
  </code></pre>
  <p>Then I do some logic</p>
  <pre><code>        writer = ExcelWriter(FilePath)
          data.to_excel(writer, 'Sheet1', index=False)
          data2.to_excel(writer, 'Sheet2', index=False)
      
          writer.save()
      return render(request, 'home.html')
  </code></pre>
  <p>When I was using the .exe ExcelWriter(&quot;location where I saved the new file&quot;) was how I returned the edited data to myself.</p>
  <p>Edit 1:</p>
  <p>I have adjusted the code to</p>
  <pre><code>        io = StringIO()
  
          writer = ExcelWriter(io)
  
  
          data.to_excel(writer, 'Sheet1', index=False)
          data2.to_excel(writer, 'Sheet2', index=False)
          response = HttpResponse(io.getvalue(), content_type='application/zip')
          response['Content-Disposition'] = 'attachment; filename=myfile.zip'
      return render(response, 'home.html')
  </code></pre>
  <p>I am now getting the error 'HttpResponse' object has no attribute 'META' not sure how to resolve this.</p>
  <p>Edit 2:
  it seems I might be able to solve this by using writer.save(response) however I am getting the error &quot;save() takes 1 positional argument but 2 were given&quot; and I am not sure how to solve this.</p>
  <pre><code>        io = StringIO()
  
          writer = ExcelWriter(io)
  
  
          data.to_excel(writer, 'Sheet1', index=False)
          data2.to_excel(writer, 'Sheet2', index=False)
          writer.save(response)
  </code></pre>
  <p>Edit 3:</p>
  <p>I am not longer having any errors appears, however the file is not getting returned to the html page. This is my current code without errors.</p>
  <pre><code>from IO import *
          io = BytesIO()
  
          writer = ExcelWriter(io)
  
  
          data.to_excel(writer, 'Sheet1', index=False)
          data2.to_excel(writer, 'Sheet2', index=False)
          writer.save()
          rFile = io.getvalue()
          response = HttpResponse(rFile,content_type='application/ms-excel')
          response['Content-Disposition'] = 'attachment; filename=temp.xls'
      return render(request, 'home.html')
  </code></pre>
  <p>Going to keep working on how to return the file to the page.</p>
  <p>Edit 4:</p>
  <p>I was able to solve this issue. Below is the code I needed to return the excel file.</p>
  <pre><code>        io = BytesIO()
  
          writer = ExcelWriter(io)
  
  
          data.to_excel(writer, 'Sheet1', index=False)
          data2.to_excel(writer, 'Sheet2', index=False)
          writer.save()
          rFile = io.getvalue()
          response = HttpResponse(rFile ,content_type='application/ms-excel')
          response['Content-Disposition'] = 'attachment; filename=temp.xls'
          return response
      return render(request, 'home.html')
  </code></pre>