title: Read from uploaded XLSX file in Python CGI script using Pandas
body: <p>I am creating a tool where either</p>
  <ol>
  <li>A new XLSX file is generated for the user to download</li>
  <li>The user can upload an XLSX file they have, I will read the contents of that file, aand use them to generate a new file for the user to download.</li>
  </ol>
  <p>I would like to make use of Pandas to read the XLSX file into a dataframe, so I can work with it easily. However, I can't get it working. Can you help me?</p>
  <p>Example extract from CGI file:</p>
  <pre><code>import pandas as pd
  import cgi
  from mako.template import Template
  from mako.lookup import TemplateLookup
  import http.cookies as Cookie
  import os
  import tempfile
  import shutil
  import sys
  
  cookie = Cookie.SimpleCookie(os.environ.get(&quot;HTTP_COOKIE&quot;))
  
  method = os.environ.get(&quot;REQUEST_METHOD&quot;, &quot;GET&quot;)
  
  templates = TemplateLookup(directories = ['templates'], output_encoding='utf-8')
  
  if method == &quot;GET&quot;: # This is for getting the page
      
      template = templates.get_template(&quot;my.html&quot;)
      sys.stdout.flush()
      sys.stdout.buffer.write(b&quot;Content-Type: text/html\n\n&quot;)
      sys.stdout.buffer.write(
          template.render())
  
  if method == &quot;POST&quot;:
  
      form = cgi.FieldStorage()
      print(&quot;Content-Type: application/vnd.ms-excel&quot;)
      print(&quot;Content-Disposition: attachment; filename=NewFile.xlsx\n&quot;)
      
      output_path = &quot;/tmp/&quot; + next(tempfile._get_candidate_names()) + '.xlsx'
      
      data = *some pandas dataframe previously created*
  
      if &quot;editfile&quot; in form:
          myfilename = form['myfile'].filename
          with open(myfilename, 'wb') as f:
              f.write(form['myfile'].file.read())                
          data = pd.read_excel(myfilename)
  
      data.to_excel(output_path)
  
      with open(path, &quot;rb&quot;) as f:
          sys.stdout.flush()
          shutil.copyfileobj(f, sys.stdout.buffer)
  
  </code></pre>
  <p>Example extract from HTML file:</p>
  <pre><code>&lt;p&gt;Press the button below to generate a new version of the xlsx file&lt;/p&gt; 
  &lt;form method=post&gt;
  &lt;p&gt;&lt;input type=submit value='Generate new version of file' name='newfile'&gt;
  &lt;div class=&quot;wrapper&quot;&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  &lt;br&gt;
  &lt;p&gt;Or upload a file.&lt;/p&gt;
  &lt;p&gt;In this case, a new file will be created using the contents of this file.&lt;/p&gt;
  &lt;form method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;
      &lt;input id=&quot;fileupload&quot; name=&quot;myfile&quot; type=&quot;file&quot; /&gt;
      &lt;input value=&quot;Upload and create new file&quot; name='editfile' type=&quot;submit&quot; /&gt;
  &lt;/form&gt;
  
  </code></pre>
  <p>This works without the <code>if &quot;editfile&quot; in form:</code> bit so I know something is going wrong when I am trying to access the file that the user has uploaded.</p>
  <p>The problem is that whilst a file is created, the created file has a file size of 0 KB and will not open in Excel. Crucially, the file that the user has uploaded can not be found in the location that I have written it out.</p>