Pandas XLSWriter - 返回而不是写

时间:2018-05-10 14:29:13

标签: python excel pandas

我想从Flask(Python)服务器返回一个Excel文件。这段代码:

<html>

<head>

  <link href="https://cdnjs.cloudflare.com/ajax/libs/rangeslider.js/2.3.0/rangeslider.min.css" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">

  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/rangeslider.js/2.3.0/rangeslider.min.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js"></script>
</head>
<main class="container">
  <div class="sliderHolder">
    <div class="wheelCat">Citizenship</div>
    <div class="sliderAll">
      <div class="sliderCitizenship">
        <input type=range value=1 min=0 max=8 step=1 id="Citizenship">
      </div>
    </div>
    <br>

    <div class="wheelCat">People</div>
    <div class="sliderAll">
      <div class="sliderPeople">
        <input type=range value=1 min=0 max=8 step=1 id="People">
      </div>
    </div>
    <br>

    <div class="wheelCat">Growth</div>
    <div class="sliderAll">
      <div class="sliderGrowth">
        <input type=range value=1 min=0 max=8 step=1 id="Growth">
      </div>
    </div>
    <br>

    <div class="wheelCat">Management</div>
    <div class="sliderAll">
      <div class="sliderManagement">
        <input type=range value=1 min=0 max=8 step=1 id="Management">
      </div>
    </div>
    <br>

    <div class="wheelCat">Relationships</div>
    <div class="sliderAll">
      <div class="sliderCitizenship">
        <input type=range value=1 min=0 max=8 step=1 id="Citizenship">
      </div>
    </div>
    <br>

    <div class="wheelCat">Health</div>
    <div class="sliderAll">
      <div class="sliderHealth">
        <input type=range value=1 min=0 max=8 step=1 id="Health">
      </div>
    </div>
    <br>

    <div class="wheelCat">Wealth</div>
    <div class="sliderAll">
      <div class="sliderWealth">
        <input type=range value=1 min=0 max=8 step=1 id="Wealth">
      </div>
    </div>
    <br>

    <div class="wheelCat">Joy</div>
    <div class="sliderAll">
      <div class="sliderJoy">
        <input type=range value=1 min=0 max=8 step=1 id="Joy">
      </div>
    </div>
    <br>

  </div>
  <div style="float:left;width:80%;">
    <canvas id="polarChart"></canvas>
  </div>
</main>

</html>

将Excel文件写入文件系统。我如何writer = pd.ExcelWriter('filename.xlsx') dataframe.to_excel(writer, index=False) writer.save() 该文件而不是写它?

1 个答案:

答案 0 :(得分:2)

您可以使用StringIOBytesIO对象将Excel数据写入内存。

此代码是从pandas文档here中复制的:

# Safe import for either Python 2.x or 3.x
try:
    from io import BytesIO
except ImportError:
    from cStringIO import StringIO as BytesIO

bio = BytesIO()

# By setting the 'engine' in the ExcelWriter constructor.
writer = ExcelWriter(bio, engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')

# Save the workbook
writer.save()

# Seek to the beginning and read to copy the workbook to a variable in memory
bio.seek(0)
workbook = bio.read()