Commit 3ea49353 authored by Remi Rampin's avatar Remi Rampin

Add Excel (.xlsx) export format

parent bd6c92ef
......@@ -48,6 +48,7 @@
</button>
<div class="dropdown-menu" aria-labelledby="dropdown-codebook">
<a href="{{ reverse_url('export_codebook_qdc', project.id) }}" class="dropdown-item">QDC (XML)</a>
<a href="{{ reverse_url('export_codebook_xlsx', project.id) }}" class="dropdown-item">Excel</a>
<a href="{{ reverse_url('export_codebook_csv', project.id) }}" class="dropdown-item">CSV</a>
<a href="{{ reverse_url('export_codebook_doc', project.id, 'html') }}" class="dropdown-item">HTML</a>
<a href="{{ reverse_url('export_codebook_doc', project.id, 'docx') }}" class="dropdown-item">DOCX</a>
......
......@@ -80,6 +80,8 @@ def make_app(config, debug=False, xsrf_cookies=True):
export.ExportCodebookXml, name='export_codebook_qdc'),
URLSpec('/project/([0-9]+)/export/codebook.csv',
export.ExportCodebookCsv, name='export_codebook_csv'),
URLSpec('/project/([0-9]+)/export/codebook.xlsx',
export.ExportCodebookXlsx, name='export_codebook_xlsx'),
URLSpec('/project/([0-9]+)/export/codebook.([a-z0-3]{2,4})',
export.ExportCodebookDoc, name='export_codebook_doc'),
URLSpec('/project/([0-9]+)/export/document/'
......
import bisect
import csv
import logging
import uuid
from xml.sax.saxutils import XMLGenerator
from xml.sax.xmlreader import AttributesNSImpl
from markupsafe import Markup
import os
import prometheus_client
import shutil
from sqlalchemy.orm import aliased, joinedload
import tempfile
from tornado.web import authenticated
import uuid
from xml.sax.saxutils import XMLGenerator
from xml.sax.xmlreader import AttributesNSImpl
import xlsxwriter
from .. import convert, __version__
from .. import database
......@@ -219,6 +222,47 @@ class ExportCodebookCsv(BaseHandler):
return self.finish()
class ExportCodebookXlsx(BaseHandler):
PROM_EXPORT.labels('codebook', 'xls').inc(0)
@authenticated
def get(self, project_id):
PROM_EXPORT.labels('codebook', 'xls').inc()
project, _ = self.get_project(project_id)
tags = list(project.tags)
self.set_header('Content-Type',
('application/vnd.openxmlformats-officedocument.'
'spreadsheetml.sheet'))
self.set_header('Content-Disposition',
'attachment; filename="codebook.xlsx"')
tmp = tempfile.mkdtemp(prefix='taguette_xlsx_')
try:
filename = os.path.join(tmp, 'codebook.xlsx')
workbook = xlsxwriter.Workbook(filename)
sheet = workbook.add_worksheet('codebook')
header = workbook.add_format({'bold': True})
sheet.write(0, 0, 'tag', header)
sheet.write(0, 1, 'description', header)
sheet.set_column(0, 0, 30.0)
sheet.set_column(1, 1, 80.0)
for row, tag in enumerate(tags, start=1):
sheet.write(row, 0, tag.path)
sheet.write(row, 1, tag.description)
workbook.close()
with open(filename, 'rb') as fp:
chunk = fp.read(4096)
self.write(chunk)
while len(chunk) == 4096:
chunk = fp.read(4096)
if chunk:
self.write(chunk)
return self.finish()
finally:
shutil.rmtree(tmp)
class ExportCodebookDoc(BaseHandler):
init_PROM_EXPORT('codebook')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment