-
-
Save netnichols/da3f60b1fb3496b44088 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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