言語商会

メモ

いや、下記は自分用の備忘録ですのでお気になさらずに。

DokuWiki

emacs

  • 文字コード明示:-*- coding:utf-8 -*-

Python

素材

広告掲示

Let's Encrypt / SSL

How to download Excel file (.xlsx) by Python CGI

import openpyxl
import sys
import os
import shutil
import tempfile
 
wb = openpyxl.Workbook()
ws = wb.active
c = ws.cell(row=5, column=5)
c.value = 3.14
 
with tempfile.NamedTemporaryFile() as tmp:
  wb.save(tmp.name)
  tmp.seek(0)
  stream = tmp.read()
 
print("Content-Type: application/octet-stream")
print('Content-Disposition: attachment; filename="results.xlsx"')
print("Content-Length: " + str(len(stream)))
print('Cache-Control: max-age=0')
print()
 
sys.stdout.flush()
sys.stdout.buffer.write(stream)
 (感想・要望・情報提供)