def count_size(wh_list, row): """Count height of image row. Args: wh_list - list with attributes, contains width and height: row - image row. """ height, width = -1, -1 for l in wh_list: if l[0] == 'width': width = float(l[1].replace('in', '')) if l[0] == 'height': height = float(l[1].replace('in', '')) if height == -1 or width == -1: width = ININTENCM height = ININTENCM if width > ININTENCM: new_width = ININTENCM new_height = height * new_width / width else: new_width = width new_height = height height_set = str(new_height) + 'in' new_name = 'image' + str(image_counter) height_suit = Style(name=new_name, family='table-row') height_suit.addElement(TableRowProperties(rowheight=height_set)) ods.automaticstyles.addElement(height_suit) row.setAttribute(attr='stylename', value=new_name) new_width = str(new_width) + 'in' new_height = height_set return new_width, new_height
def create_blank_ods_with_styles(): """Create a dmutils.ods.SpreadSheet pre-configured with some default styles, ready for population with data appropriate for the subclass View. Modifications here (except adding styles) are likely breaking changes.""" spreadsheet = ods.SpreadSheet() # Add the font we will use for the entire spreadsheet. spreadsheet.add_font(FontFace(name="Arial", fontfamily="Arial")) # Add some default styles for columns. spreadsheet.add_style("col-default", "table-column", ( TableColumnProperties(breakbefore="auto"), ), parentstylename="Default") spreadsheet.add_style("col-wide", "table-column", ( TableColumnProperties(columnwidth="150pt", breakbefore="auto"), ), parentstylename="Default") spreadsheet.add_style("col-extra-wide", "table-column", ( TableColumnProperties(columnwidth="300pt", breakbefore="auto"), ), parentstylename="Default") # Add some default styles for rows. spreadsheet.add_style("row-default", "table-row", ( TableRowProperties(breakbefore="auto", useoptimalrowheight="false"), ), parentstylename="Default") spreadsheet.add_style("row-tall", "table-row", ( TableRowProperties(breakbefore="auto", rowheight="30pt", useoptimalrowheight="false"), ), parentstylename="Default") spreadsheet.add_style("row-tall-optimal", "table-row", ( TableRowProperties(breakbefore="auto", rowheight="30pt", useoptimalrowheight="true"), ), parentstylename="Default") # Add some default styles for cells. spreadsheet.add_style("cell-default", "table-cell", ( TableCellProperties(wrapoption="wrap", verticalalign="top"), TextProperties(fontfamily="Arial", fontnameasian="Arial", fontnamecomplex="Arial", fontsize="11pt"), ), parentstylename="Default") spreadsheet.add_style("cell-header", "table-cell", ( TableCellProperties(wrapoption="wrap", verticalalign="top"), TextProperties(fontfamily="Arial", fontnameasian="Arial", fontnamecomplex="Arial", fontsize="11pt", fontweight="bold"), ), parentstylename="Default") return spreadsheet
def __init__(self): self.doc = OpenDocumentSpreadsheet() #styles self.itemRowStyle1 = Style(name="itemRowStyle", family="table-row") self.itemRowStyle1.addElement(TableRowProperties(rowheight="7mm")) self.doc.automaticstyles.addElement(self.itemRowStyle1) self.itemRowStyle3 = Style(name="itemRowStyle", family="table-row") self.itemRowStyle3.addElement(TableRowProperties(rowheight="30mm")) self.doc.automaticstyles.addElement(self.itemRowStyle3) self.colStyle30 = Style(name="colStyle30", family="table-column") self.colStyle30.addElement(TableColumnProperties(columnwidth="25mm")) self.doc.automaticstyles.addElement(self.colStyle30) self.colStyle40 = Style(name="colStyle40", family="table-column") self.colStyle40.addElement(TableColumnProperties(columnwidth="40mm")) self.doc.automaticstyles.addElement(self.colStyle40) self.colStyle50 = Style(name="colStyle50", family="table-column") self.colStyle50.addElement(TableColumnProperties(columnwidth="50mm")) self.doc.automaticstyles.addElement(self.colStyle50) self.colStyle200 = Style(name="colStyle200", family="table-column") self.colStyle200.addElement(TableColumnProperties(columnwidth="200mm")) self.doc.automaticstyles.addElement(self.colStyle200) self.cellStyle1 = Style(name="cellStyle1",family="table-cell", parentstylename='Standard', displayname="middle") self.cellStyle1.addElement(ParagraphProperties(textalign="center")) self.cellStyle1.addElement(TableCellProperties(verticalalign="middle")) self.cellStyle1.addElement(TableCellProperties(wrapoption="wrap")) self.doc.automaticstyles.addElement(self.cellStyle1) self.hdrStyle = Style(name="hdrStyle",family="table-cell", parentstylename='Standard', displayname="middle") self.hdrStyle.addElement(ParagraphProperties(textalign="center")) self.hdrStyle.addElement(TextProperties(fontweight="bold")) self.hdrStyle.addElement(TableCellProperties(verticalalign="middle")) self.doc.automaticstyles.addElement(self.hdrStyle)
def count_height(row, cell): """Counting height that shows all text in cell. This functions uses width of text-column and font size. Args: row - current row. cell - current cell. """ style_name = cell.getAttribute('stylename') try: style = saved_styles[style_name] text_prop = style.getElementsByType(TextProperties) try: text_prop = text_prop[0] font_size = str(text_prop.getAttribute('fontsize')) font_size = font_size.replace('pt', '') font_size = int(font_size) except IndexError: font_size = 10 except KeyError: font_size = 10 symbols_in_string = PTINTENCM // font_size + 1 length = 0 for p in cell.getElementsByType(P): length += len(p.__str__()) height = font_size * (length // symbols_in_string + 1) + 4 height = str(height) + 'pt' new_name = 'heightsuit' + height height_suit = Style(name=new_name, family='table-row') height_suit.addElement(TableRowProperties(rowheight=height)) ods.automaticstyles.addElement(height_suit) row.setAttribute(attr='stylename', value=new_name)
def insert_table_(self, ar, column_names=None, table_width=180): # logger.info("20160330 insert_table(%s)", ar) ar.setup_from(self.ar) columns, headers, widths = ar.get_field_info(column_names) widths = map(int, widths) tw = sum(widths) # specifying relative widths doesn't seem to work (and that's # a pity because absolute widths requires us to know the # table_width). use_relative_widths = False if use_relative_widths: width_specs = ["%d*" % (w * 100 / tw) for w in widths] else: width_specs = ["%dmm" % (table_width * w / tw) for w in widths] doc = OpenDocumentText() def add_style(**kw): st = Style(**cleankw(kw)) doc.styles.addElement(st) self.my_styles.append(st) return st table_style_name = str(ar.actor) st = add_style(name=table_style_name, family="table", parentstylename="Default") st.addElement( TableProperties(align="margins", maybreakbetweenrows="0")) # create some *visible* styles st = add_style(name="Table Contents", family="paragraph", parentstylename="Default") st.addElement(ParagraphProperties(numberlines="false", linenumber="0")) st = add_style(name="Number Cell", family="paragraph", parentstylename="Table Contents") st.addElement(ParagraphProperties( numberlines="false", textalign="end", justifysingleword="true", linenumber="0")) dn = "Table Column Header" st = self.stylesManager.styles.getStyle(dn) if st is None: st = add_style(name=dn, family="paragraph", parentstylename="Table Contents") st.addElement( ParagraphProperties(numberlines="false", linenumber="0")) st.addElement(TextProperties(fontweight="bold")) dn = "Bold Text" st = self.stylesManager.styles.getStyle(dn) if st is None: st = add_style(name=dn, family="text", parentstylename="Default") #~ st = add_style(name=dn, family="text") st.addElement(TextProperties(fontweight="bold")) if False: dn = "L1" st = self.stylesManager.styles.getStyle(dn) if st is None: st = ListStyle(name=dn) doc.styles.addElement(st) p = ListLevelProperties( listlevelpositionandspacemode="label-alignment") st.addElement(p) #~ label-followed-by="listtab" text:list-tab-stop-position="1.27cm" fo:text-indent="-0.635cm" fo:margin-left="1.27cm"/> p.addElement(ListLevelLabelAlignment(labelfollowedby="listtab", listtabstopposition="1.27cm", textindent="-0.635cm", marginleft="1.27cm" )) self.my_styles.append(st) #~ list_style = add_style(name=dn, family="list") bullet = text.ListLevelStyleBullet( level=1, stylename="Bullet_20_Symbols", bulletchar=u"•") #~ bullet = text.ListLevelStyleBullet(level=1,stylename="Bullet_20_Symbols",bulletchar=u"*") #~ <text:list-level-style-bullet text:level="1" text:style-name="Bullet_20_Symbols" text:bullet-char="•"> st.addElement(bullet) # create some automatic styles def add_style(**kw): st = Style(**cleankw(kw)) doc.automaticstyles.addElement(st) self.my_automaticstyles.append(st) return st cell_style = add_style(name="Lino Cell Style", family="table-cell") cell_style.addElement(TableCellProperties( paddingleft="1mm", paddingright="1mm", paddingtop="1mm", paddingbottom="0.5mm", border="0.002cm solid #000000")) header_row_style = add_style( name="Lino Header Row", family="table-row", parentstylename=cell_style) header_row_style.addElement( TableRowProperties(backgroundcolor="#eeeeee")) total_row_style = add_style( name="Lino Total Row", family="table-row", parentstylename=cell_style) total_row_style.addElement( TableRowProperties(backgroundcolor="#ffffff")) table = Table(name=table_style_name, stylename=table_style_name) table_columns = TableColumns() table.addElement(table_columns) table_header_rows = TableHeaderRows() table.addElement(table_header_rows) table_rows = TableRows() table.addElement(table_rows) # create table columns and automatic table-column styles for i, fld in enumerate(columns): #~ print 20120415, repr(fld.name) name = str(ar.actor) + "." + str(fld.name) cs = add_style(name=name, family="table-column") if use_relative_widths: cs.addElement( TableColumnProperties(relcolumnwidth=width_specs[i])) else: cs.addElement( TableColumnProperties(columnwidth=width_specs[i])) #~ cs.addElement(TableColumnProperties(useoptimalcolumnwidth='true')) #~ k = cs.getAttribute('name') #~ renderer.stylesManager.styles[k] = toxml(e) #~ doc.automaticstyles.addElement(cs) #~ self.my_automaticstyles.append(cs) table_columns.addElement(TableColumn(stylename=name)) def fldstyle(fld): #~ if isinstance(fld,ext_store.VirtStoreField): #~ fld = fld.delegate if isinstance(fld, NumberFieldElement): return "Number Cell" return "Table Contents" def value2cell(ar, i, fld, val, style_name, tc): # if i == 0: # logger.info("20160330a value2cell(%s, %s)", fld.__class__, val) txt = fld.value2html(ar, val) # if i == 0: # logger.info("20160330b value2cell(%s)", E.tostring(txt)) p = text.P(stylename=style_name) html2odf(txt, p) try: tc.addElement(p) except Exception as e: dd.logger.warning("20120614 addElement %s %s %r : %s", i, fld, val, e) #~ print 20120614, i, fld, val, e #~ yield P(stylename=tablecontents,text=text) # create header row #~ hr = TableRow(stylename=HEADER_ROW_STYLE_NAME) hr = TableRow(stylename=header_row_style) table_header_rows.addElement(hr) for h in headers: #~ for fld in fields: #~ tc = TableCell(stylename=CELL_STYLE_NAME) tc = TableCell(stylename=cell_style) tc.addElement(text.P( stylename="Table Column Header", #~ text=force_text(fld.field.verbose_name or fld.name))) text=force_text(h))) hr.addElement(tc) sums = [fld.zero for fld in columns] for row in ar.data_iterator: #~ for grp in ar.group_headers(row): #~ raise NotImplementedError() tr = TableRow() has_numeric_value = False for i, fld in enumerate(columns): #~ tc = TableCell(stylename=CELL_STYLE_NAME) tc = TableCell(stylename=cell_style) #~ if fld.field is not None: v = fld.field._lino_atomizer.full_value_from_object(row, ar) stylename = fldstyle(fld) if v is None: tc.addElement(text.P(stylename=stylename, text='')) else: value2cell(ar, i, fld, v, stylename, tc) nv = fld.value2num(v) if nv != 0: sums[i] += nv has_numeric_value = True #~ sums[i] += fld.value2num(v) tr.addElement(tc) if has_numeric_value or not ar.actor.hide_zero_rows: table_rows.addElement(tr) if not ar.actor.hide_sums: if sums != [fld.zero for fld in columns]: tr = TableRow(stylename=total_row_style) table_rows.addElement(tr) sums = {fld.name: sums[i] for i, fld in enumerate(columns)} for i, fld in enumerate(columns): tc = TableCell(stylename=cell_style) stylename = fldstyle(fld) p = text.P(stylename=stylename) e = fld.format_sum(ar, sums, i) html2odf(e, p) tc.addElement(p) #~ if len(txt) != 0: #~ msg = "html2odf() returned " #~ logger.warning(msg) #~ txt = tuple(html2odf(fld.format_sum(ar,sums,i),p)) #~ assert len(txt) == 1 #~ tc.addElement(text.P(stylename=stylename,text=txt[0])) tr.addElement(tc) doc.text.addElement(table) return toxml(table)
def par_classe(classes, fileout): ods = OpenDocumentSpreadsheet() style_civilite = Style(parent=ods.automaticstyles, name='col_civilite', family='table-column') TableColumnProperties(parent=style_civilite, columnwidth='1cm') style_nom = Style(parent=ods.automaticstyles, name='col_nom', family='table-column') TableColumnProperties(parent=style_nom, columnwidth='4.5cm') style_date = Style(parent=ods.automaticstyles, name='col_date', family='table-column') TableColumnProperties(parent=style_date, columnwidth='3.2cm') style_internat = Style(parent=ods.automaticstyles, name='col_internat', family='table-column') TableColumnProperties(parent=style_internat, columnwidth='3.2cm') style_classe = Style(parent=ods.automaticstyles, name='col_classe', family='table-column') TableColumnProperties(parent=style_classe, columnwidth='3.2cm') style_etat_voeu = Style(parent=ods.automaticstyles, name='col_etat_voeu', family='table-column') TableColumnProperties(parent=style_etat_voeu, columnwidth='4cm') style_titre = Style(parent=ods.automaticstyles, name='cell_titre', family='table-cell') TextProperties(parent=style_titre, fontweight='bold', fontsize='14pt') ParagraphProperties(parent=style_titre, textalign='center') style_ligne_titre = Style(parent=ods.automaticstyles, name='ligne_titre', family='table-row') TableRowProperties(parent=style_ligne_titre, rowheight='8mm') style_entete = Style(parent=ods.automaticstyles, name='cell_entete', family='table-cell') TextProperties(parent=style_entete, fontweight='bold') number_style_date_format = odf.number.DateStyle(parent=ods.automaticstyles, name='date_number') odf.number.Day(parent=number_style_date_format, style='long') odf.number.Text(parent=number_style_date_format, text="/") odf.number.Month(parent=number_style_date_format, style='long') odf.number.Text(parent=number_style_date_format, text="/") odf.number.Year(parent=number_style_date_format, style='long') style_date_format = Style(parent=ods.automaticstyles, name='cell_date', family='table-cell', datastylename=number_style_date_format) for classe in classes: table = Table(name=str(classe)) table.addElement(TableColumn(stylename=style_civilite)) # Sexe table.addElement(TableColumn(stylename=style_nom)) # Nom table.addElement(TableColumn(stylename=style_nom)) # Prénom table.addElement( TableColumn(stylename=style_date)) # Date de naissance table.addElement(TableColumn(stylename=style_internat)) # Internat table.addElement(TableColumn(stylename=style_etat_voeu)) # État vœu table.addElement(TableColumn(stylename=style_nom)) # E-mail table.addElement(TableColumn(stylename=style_nom)) # Téléphone table.addElement(TableColumn(stylename=style_nom)) # Mobile # En-tête de la feuille tr = TableRow(parent=table, stylename=style_ligne_titre) cell = TableCell(parent=tr, numbercolumnsspanned=9, numberrowsspanned=1, valuetype='string', stylename=style_titre) cell.addElement(P(text=str(classe))) CoveredTableCell(parent=tr, numbercolumnsrepeated=8) tr = TableRow(parent=table) TableCell(parent=tr) # Sexe P(parent=TableCell(parent=tr, valuetype='string', stylename=style_entete), text="Nom") P(parent=TableCell(parent=tr, valuetype='string', stylename=style_entete), text="Prénom") P(parent=TableCell(parent=tr, valuetype='string', stylename=style_entete), text="Date de naissance") P(parent=TableCell(parent=tr, valuetype='string', stylename=style_entete), text="Internat") P(parent=TableCell(parent=tr, valuetype='string', stylename=style_entete), text="État Parcoursup") P(parent=TableCell(parent=tr, valuetype='string', stylename=style_entete), text="E-mail") P(parent=TableCell(parent=tr, valuetype='string', stylename=style_entete), text="Téléphone") P(parent=TableCell(parent=tr, valuetype='string', stylename=style_entete), text="Mobile") for etudiant in classe.admissions().order_by('nom'): tr = TableRow() table.addElement(tr) TableCell(parent=tr, valuetype='string').addElement( P(text=etudiant.get_sexe_display())) TableCell(parent=tr, valuetype='string').addElement(P(text=etudiant.nom)) TableCell(parent=tr, valuetype='string').addElement(P(text=etudiant.prenom)) cell = TableCell(valuetype='date', datevalue=str(etudiant.date_naissance), stylename=style_date_format) cell.addElement(P(text=etudiant.date_naissance)) tr.addElement(cell) cell = TableCell(valuetype='string') if etudiant.proposition_actuelle.internat: cell.addElement(P(text="Interne")) tr.addElement(cell) TableCell(parent=tr, valuetype='string').addElement( P(text=etudiant.proposition_actuelle.get_etat_display())) TableCell(parent=tr, valuetype='string').addElement(P(text=etudiant.email)) TableCell(parent=tr, valuetype='string').addElement( P(text=etudiant.telephone)) TableCell(parent=tr, valuetype='string').addElement( P(text=etudiant.telephone_mobile)) ods.spreadsheet.addElement(table) # Liste générale pour l'infirmerie table = Table(name="Infirmerie") ods.spreadsheet.addElement(table) table.addElement(TableColumn(stylename=style_civilite)) # Sexe table.addElement(TableColumn(stylename=style_nom)) # Nom table.addElement(TableColumn(stylename=style_nom)) # Prénom table.addElement(TableColumn(stylename=style_classe)) # Classe table.addElement(TableColumn(stylename=style_date)) # Date de naissance table.addElement(TableColumn(stylename=style_internat)) # Internat # En-tête de la feuille tr = TableRow(parent=table, stylename=style_ligne_titre) cell = TableCell(parent=tr, numbercolumnsspanned=9, numberrowsspanned=1, valuetype='string', stylename=style_titre) cell.addElement(P(text="Liste de tous les étudiants admis")) tr = TableRow(parent=table) TableCell(parent=tr) # Sexe P(parent=TableCell(parent=tr, valuetype='string', stylename=style_entete), text="Nom") P(parent=TableCell(parent=tr, valuetype='string', stylename=style_entete), text="Prénom") P(parent=TableCell(parent=tr, valuetype='string', stylename=style_entete), text="Classe") P(parent=TableCell(parent=tr, valuetype='string', stylename=style_entete), text="Date de naissance") P(parent=TableCell(parent=tr, valuetype='string', stylename=style_entete), text="Internat") for etudiant in Etudiant.objects.filter( proposition_actuelle__isnull=False, proposition_actuelle__date_demission__isnull=True).order_by( 'nom', 'prenom'): tr = TableRow(parent=table) TableCell(parent=tr, valuetype='string').addElement( P(text=etudiant.get_sexe_display())) TableCell(parent=tr, valuetype='string').addElement(P(text=etudiant.nom)) TableCell(parent=tr, valuetype='string').addElement(P(text=etudiant.prenom)) TableCell(parent=tr, valuetype='string').addElement( P(text=str(etudiant.proposition_actuelle.classe))) cell = TableCell(valuetype='date', datevalue=str(etudiant.date_naissance), stylename=style_date_format) cell.addElement(P(text=etudiant.date_naissance)) tr.addElement(cell) cell = TableCell(valuetype='string') if etudiant.proposition_actuelle.internat: cell.addElement(P(text="Interne")) tr.addElement(cell) ods.write(fileout)