def _writeodsitem(self, col, value): """Uses col and value to make entry in ods file (using self.currentrow)""" # column numbering starts at 1 for spreadsheet column A curcol = 0 ### restarts at beginning of row for each entry targetcol = col r = self.currentrow for c in r.childNodes: if c.tagName != 'table:table-cell': continue repeatedcols = c.getAttribute('numbercolumnsrepeated') repcount = int(repeatedcols) if repeatedcols else 1 if curcol + repcount >= targetcol: break curcol += repcount else: pass ### got to end of row without finding desired column; add more cells? or report bad col? ### doesn't preserve background color (see Time Zones for example) countbefore = targetcol - curcol - 1 countafter = curcol + repcount - targetcol if countbefore > 0: c1 = TableCell() c1.setAttribute('numbercolumnsrepeated', str(countbefore)) c.setAttribute('numbercolumnsrepeated', str(countafter + 1)) x = r.insertBefore(c1, c) if countafter > 0: c.setAttribute('numbercolumnsrepeated', '1') c2 = TableCell() c2.setAttribute('numbercolumnsrepeated', str(countafter)) if c == r.lastChild: x = r.appendChild(c2) else: x = r.insertBefore(c2, c.nextSibling) if c.hasChildNodes(): ### perhaps should test that child is text:p and its child is Text c.firstChild.firstChild.data = value else: c.addElement(P(text=value))
def AddCell(self, attributes=None, rowspan=1, colspan=1): tc = TableCell(attributes=attributes) tc.number = self.nextcell self.nextcell += colspan tc.setAttribute('numberrowsspanned', str(rowspan)) tc.setAttribute('numbercolumnsrepeated', str(colspan)) tc.setAttribute('numbercolumnsspanned', str(colspan)) self.row.addElement(tc) return tc
def classe_resultats_odf(request, resultats): response = HttpResponse( content_type='application/vnd.oasis.opendocument.spreadsheet') response[ 'Content-Disposition'] = 'attachment; filename="resultats_{}.ods"'.format( resultats['classe']) ods = OpenDocumentSpreadsheet() # Style numérique pour les notes style_number_note = NumberStyle(name="Note") ods.styles.addElement(style_number_note) Number(parent=style_number_note, minintegerdigits=1, decimalplaces=2) style_note = Style(datastylename=style_number_note, parent=ods.styles, name="Note", family='table-cell') # Style pour le rang style_number_rang = NumberStyle(name="Rang", parent=ods.styles) Number(parent=style_number_rang, minintegerdigits=1, decimalplaces=0) style_rang = Style(datastylename=style_number_rang, parent=ods.styles, name="Rang", family='table-column') for matiere, ens_resultats in resultats['enseignements'].items(): table = Table(name="{} - {}".format(resultats['classe'], matiere), parent=ods.spreadsheet) # Création des colonnes table.addElement(TableColumn()) # Étudiant table.addElement(TableColumn()) # Moyenne table.addElement(TableColumn(stylename=style_rang)) # Rang for _ in range(len(resultats['semaines'])): table.addElement(TableColumn()) # Ligne d'en-tête th = TableHeaderRows(parent=table) tr = TableRow(parent=th) P(parent=TableCell(parent=tr, valuetype='string'), text="Étudiant") P(parent=TableCell(parent=tr, valuetype='string'), text="Moyenne") P(parent=TableCell(parent=tr, valuetype='string'), text="Rang") for semaine in resultats['semaines']: P(parent=TableCell(parent=tr, valuetype='string'), text=semaine.numero) # Ligne pour chaque étudiant for etudiant, etu_resultats in ens_resultats['etudiants'].items(): tr = TableRow(parent=table) # Nom de l'étudiant P(parent=TableCell(parent=tr, valuetype='string'), text=str(etudiant)) # Moyenne de l'étudiant P(parent=TableCell(parent=tr, valuetype='float', value=etu_resultats['moyenne'], stylename=style_note), text="{:.2f}".format(etu_resultats['moyenne'])) # Rang de l'étudiant P(parent=TableCell(parent=tr, valuetype='float', value=etu_resultats['rang']), text="{}".format(etu_resultats['rang'])) # Notes for note in etu_resultats['notes']: tc = TableCell(parent=tr) if isinstance(note, list) and len(note) == 1: note = note[0] if isinstance(note, Note): if note.est_note(): tc.setAttribute('valuetype', 'float') tc.setAttribute('value', note.value) tc.setAttribute('stylename', style_note) P(text="{:.2f}".format(note), parent=tc) elif isinstance(note, list): P(text=', '.join([ "{:.2f}".format(n) for n in note if isinstance(n, Note) ]), parent=tc) ods.write(response) return response