Skip to content

Instantly share code, notes, and snippets.

@netnichols
Last active January 3, 2016 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netnichols/da3f60b1fb3496b44088 to your computer and use it in GitHub Desktop.
Save netnichols/da3f60b1fb3496b44088 to your computer and use it in GitHub Desktop.
#!/bin/env python
import bottle
import openpyxl
@bottle.route('/')
def index():
return """
<h1>openpyxl</h1>
<table>
<tr><td>Version</td><td>%s</td></tr>
<tr><td>LXML</td><td>%s</td></tr>
</table>
<ul>
<li><a href="/openpyxl">openpyxl</a></li>
<li><a href="/xlsxwriter">xlsxwriter</a></li>
</ul>
""" % (openpyxl.__version__, str(openpyxl.LXML))
@bottle.route('/openpyxl')
def test_openpyxl():
from openpyxl import Workbook
from openpyxl.writer.excel import save_virtual_workbook
wb = Workbook()
ws = wb.get_active_sheet()
ws.cell('A1').value = 'Original'
ws.cell('A1').style.font.bold = True
ws.cell('B1').value = 'Translation'
ws.cell('B1').style.font.bold = True
buf = save_virtual_workbook(wb)
header = dict()
header['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
header['Content-Disposition'] = 'attachment; filename="%s"' % 'test.xlsx'
return bottle.HTTPResponse(buf, header=header)
@bottle.route('/xlsxwriter')
def text_xlsxwriter():
import StringIO
import xlsxwriter
output = StringIO.StringIO()
workbook = xlsxwriter.Workbook(output, {'in_memory': True})
worksheet = workbook.add_worksheet()
worksheet.write(0, 0, 'Hello, world!')
workbook.close()
output.seek(0)
header = dict()
header['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
header['Content-Disposition'] = 'attachment; filename="%s"' % 'test.xlsx'
return bottle.HTTPResponse(output, header=header)
bottle.run(host='0.0.0.0', port=8888)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment