def init(LOdoc, numfonts=1): totalwid = 6800 #6.8inches #compute column widths f = min(numfonts,4) ashare = 4*(6-f) dshare = 2*(6-f) bshare = 100 - 2*ashare - dshare awid = totalwid * ashare // 100 dwid = totalwid * dshare // 100 bwid = totalwid * bshare // (numfonts * 100) # create styles for table, for columns (one style for each column width) # and for one cell (used for everywhere except where background changed) tstyle = Style(name="Table1", family="table") tstyle.addElement(TableProperties(attributes={'width':str(totalwid/1000.)+"in", 'align':"left"})) LOdoc.automaticstyles.addElement(tstyle) tastyle = Style(name="Table1.A", family="table-column") tastyle.addElement(TableColumnProperties(attributes={'columnwidth':str(awid/1000.)+"in"})) LOdoc.automaticstyles.addElement(tastyle) tbstyle = Style(name="Table1.B", family="table-column") tbstyle.addElement(TableColumnProperties(attributes={'columnwidth':str(bwid/1000.)+"in"})) LOdoc.automaticstyles.addElement(tbstyle) tdstyle = Style(name="Table1.D", family="table-column") tdstyle.addElement(TableColumnProperties(attributes={'columnwidth':str(dwid/1000.)+"in"})) LOdoc.automaticstyles.addElement(tdstyle) ta1style = Style(name="Table1.A1", family="table-cell") ta1style.addElement(TableCellProperties(attributes={'padding':"0.035in", 'border':"0.05pt solid #000000"})) LOdoc.automaticstyles.addElement(ta1style) # text style used with non-<em> text t1style = Style(name="T1", family="text") t1style.addElement(TextProperties(attributes={'color':"#999999" })) LOdoc.automaticstyles.addElement(t1style) # create styles for Title, Subtitle tstyle = Style(name="Title", family="paragraph") tstyle.addElement(TextProperties(attributes={'fontfamily':"Arial",'fontsize':"24pt",'fontweight':"bold" })) LOdoc.styles.addElement(tstyle) ststyle = Style(name="Subtitle", family="paragraph") ststyle.addElement(TextProperties(attributes={'fontfamily':"Arial",'fontsize':"18pt",'fontweight':"bold" })) LOdoc.styles.addElement(ststyle)
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 save(self, filename, i_max = None, j_max = None): ''' save table in ods format ''' if not i_max: i_max = self.table.i_max if not j_max: j_max = self.table.j_max # update cells text self.table.updateTable(i_max, j_max) # create new odf spreadsheet odfdoc = OpenDocumentSpreadsheet() # set direction style rtl = Style(name = "dir", family = "table") if self.table.direction == 'rtl': rtl.addElement(TableProperties(writingmode="rl-tb")) odfdoc.automaticstyles.addElement(rtl) # create the table table = Table(name = "sheet 1", stylename = 'dir') # default style ts = Style(name = "ts", family = "table-cell") ts.addElement(TextProperties(fontfamily = SodsCell().font_family, fontsize = SodsCell().font_size)) odfdoc.styles.addElement(ts) # create columns for j in range(1, j_max): colname = "col" + str(j) c = self.table.getCellAt(0, j) width = c.column_width cs = Style(name = colname, family = "table-column") cs.addElement(TableColumnProperties(columnwidth = width, breakbefore = "auto")) odfdoc.automaticstyles.addElement(cs) table.addElement(TableColumn(stylename = colname, defaultcellstylename = "ts")) # make sure values are up to date # loop and update the cells value for i in range(1, i_max): # create new ods row tr = TableRow() table.addElement(tr) # create default data styles for dates and numbers ncs = NumberStyle(name="ncs") ncs.addElement(Number(decimalplaces="2", minintegerdigits="1", grouping="true")) odfdoc.styles.addElement(ncs) ncs2 = NumberStyle(name="ncs2") ncs2.addElement(Number(decimalplaces="0", minintegerdigits="1", grouping="false")) odfdoc.styles.addElement(ncs2) dcs = DateStyle(name="dcs") dcs.addElement(Year(style='long')) dcs.addElement(Text(text = '-')) dcs.addElement(Month(style='long')) dcs.addElement(Text(text = '-')) dcs.addElement(Day(style='long')) odfdoc.styles.addElement(dcs) for j in range(1, j_max): # update the cell text and condition cell = self.table.encodeColName(j) + str(i) c = self.table.getCellAt(i, j) # chose datastylename if c.value_type == 'date': datastylename = "dcs" else: if c.format == "": datastylename = "ncs2" if c.format == "#,##0.00": datastylename = "ncs" # get cell style id if (c.condition): style_id = (datastylename + c.color + c.font_size + c.font_family + c.background_color + c.border_top + c.border_bottom + c.border_left + c.border_right + c.condition_color + c.condition_background_color) else: style_id = (datastylename + c.color + c.font_size + c.font_family + c.background_color + c.border_top + c.border_bottom + c.border_left + c.border_right) # set ods style style_name = self.getStyle(c, cell, datastylename, style_id, odfdoc) # create new ods cell if (c.formula and c.formula[0] == '=' and c.formula[:4] != '=uni'): if self.table.isFloat(c.value): tc = TableCell(valuetype = c.value_type, formula = c.formula, value = float(c.value), stylename = style_name) else: tc = TableCell(valuetype = c.value_type, formula = c.formula, value = 0, stylename = style_name) elif (c.value_type == 'date'): tc = TableCell(valuetype = c.value_type, datevalue = c.date_value, stylename = style_name) elif (c.value_type == 'float') and self.table.isFloat(c.value): tc = TableCell(valuetype = c.value_type, value = float(c.value), stylename = style_name) else: tc = TableCell(valuetype = 'string', stylename = style_name) # set ods text tc.addElement(P(text = str(escape(c.text), 'utf-8'))) tr.addElement(tc) odfdoc.spreadsheet.addElement(table) odfdoc.save(filename)