def create_table(labels, results): document = odf_new_document('spreadsheet') body = document.get_body() table = odf_create_table("results") row = odf_create_row() rnbr = 1 for lang in sorted(results): cell = odf_create_cell() cell.set_value(lang, text=lang) row.set_cell(rnbr, cell) rnbr += 1 table.set_row(0, row) lnbr = 1 for i in range(len(labels)): row = odf_create_row() cell = odf_create_cell() cell.set_value(labels[i], text=labels[i]) row.set_cell(0, cell) rnbr = 1 for lang in sorted(results): cell = odf_create_cell() val = results[lang][i] cell.set_value(val, text="%.5f" % val, cell_type="float") row.set_cell(rnbr, cell) rnbr += 1 table.set_row(lnbr, row) lnbr += 1 body.append(table) document.save(target="__res2.ods", pretty=True)
def test_get_table_width_after(self): table = odf_create_table(u"Empty") self.assertEqual(table.get_width(), 0) self.assertEqual(table.get_height(), 0) # The first row creates the columns table.append_row(odf_create_row(width=5)) self.assertEqual(table.get_width(), 5) self.assertEqual(table.get_height(), 1) # The subsequent ones don't table.append_row(odf_create_row(width=5)) self.assertEqual(table.get_width(), 5) self.assertEqual(table.get_height(), 2)
def test_get_table_width_after(self): table = odf_create_table(u"Empty") self.assertEqual(table.get_table_width(), 0) self.assertEqual(table.get_table_height(), 0) # The first row creates the columns table.append_row(odf_create_row(width=5)) self.assertEqual(table.get_table_width(), 5) self.assertEqual(table.get_table_height(), 1) # The subsequent ones don't table.append_row(odf_create_row(width=5)) self.assertEqual(table.get_table_width(), 5) self.assertEqual(table.get_table_height(), 2)
def GET(self, resource, context): if lpod_is_install is False: msg = ERROR(u'Please install LPOD') return context.come_back(msg) document = odf_new_document_from_type('text') body = document.get_body() root = context.root table = odf_create_table(u"Table 1", width=5, height=1, style='table-cell') for brain in root.search(format='product').get_documents(): # Get product product = root.get_resource(brain.abspath) cover = product.get_resource(product.get_property('cover')) # Add line row = odf_create_row(width=5) cell = odf_create_cell(u"") file = context.database.fs.open(cover.handler.key) local_uri = document.add_file(file) image_frame = odf_create_image_frame(local_uri, size=('5cm', '5cm'), position=('0cm', '0cm'), anchor_type='as-char') paragraph = cell.get_element('text:p') paragraph.append(image_frame) cell.append(paragraph) row.set_cell(0, cell) row.set_cell_value(1, brain.reference) row.set_cell_value(2, brain.title) row.set_cell_value(3, u'%s' % product.get_price_without_tax()) table.append_row(row) # Get declinations for d in product.search_resources(cls=Declination): price = d.parent.get_price_without_tax(id_declination=d.name, pretty=True) row = odf_create_row(width=5) row.set_cell_value(1, 'reference') row.set_cell_value(2, d.get_declination_title()) row.set_cell_value(3, u'%s' % price) row.set_cell_value(4, d.get_property('stock-quantity')) table.append(row) body.append(table) f = StringIO() document.save(f) content = f.getvalue() f.close() context.set_content_type('application/vnd.oasis.opendocument.text') context.set_content_disposition('attachment', 'export.odt') return content
def create_table_summary(document, data, headers=None, style=None): table = odf_create_table(u"Table") # Insert headers and apply base cell style to the cells if headers: odt_row = odf_create_row() odt_row.set_values(headers) for cell in odt_row.traverse(): cell.set_style(STYLES[TABLE_CELL_BASE]) odt_row.set_cell(x=cell.x, cell=cell) table.set_row(0, odt_row) for indx, row in enumerate(data): odt_row = odf_create_row() values = [] for val in row: if not val: values.append(u"") if isinstance(val, (basestring, unicode, int, float)): values.append(val) # In assessment summary table score is a tuple (conclusion, color) if isinstance(val, tuple): values.append(val[0]) odt_row.set_values(values) # Color the cell, based on the score for j, cell in enumerate(odt_row.traverse()): # first column from row gets base cell style if j == 0: cell.set_style(STYLES[TABLE_CELL_BASE]) odt_row.set_cell(x=cell.x, cell=cell) continue color_val = row[j][1] colored_style = "{}{}".format(TABLE_CELL_AS_VALUE, color_val) # other rows need too be colored cell.set_style(STYLES[colored_style]) odt_row.set_cell(x=cell.x, cell=cell) table.set_row(indx + 1, odt_row) # apply_table_cell_base_style(document, table) return table
def test_all(self): row = odf_create_row(1, repeated=3, style=u"ro1") expected = ('<table:table-row table:number-rows-repeated="3" ' 'table:style-name="ro1">' '<table:table-cell/>' '</table:table-row>') self.assertEqual(row.serialize(), expected)
def setUp(self): row = odf_create_row(width=2, repeated=3, style=u"ro1") # Add repeated cell row.append_element(odf_create_cell(1, repeated=2)) # Add regular cell row.append_element(odf_create_cell(style=u"ce1")) self.row = row
def setUp(self): row = odf_create_row(width=2, repeated=3, style=u"ro1") # Add repeated cell row.append(odf_create_cell(1, repeated=2)) # Add regular cell row.append(odf_create_cell(style=u"ce1")) self.row = row
def createTable(csv_path, table): csv_file = open(csv_path, 'r') lnbr = 0 while 1: line = csv_file.readline() if line == "": break #lnbr = lnbr + 1 row = odf_create_row() vals = line.split(",") rnbr = 0 for val in vals: cell = odf_create_cell() if lnbr < 2 or rnbr < 1: #table.set_value((rnbr, lnbr), val) cell.set_value(val, text=val) else: #table.set_value((rnbr, lnbr), float(val)) cell.set_value(float(val), text="%.5f" % float(val), cell_type="float") row.set_cell(rnbr, cell) rnbr = rnbr + 1 table.set_row(lnbr, row) lnbr = lnbr + 1
def action_export(self, resource, context, form): root = context.root results = context.root.search(format=self.export_resource.class_id) if not len(results): context.message = ERR_NO_DATA return context.query['batch_start'] = context.query['batch_size'] = 0 # Create ODS header_style = odf_create_style('table-cell', area='text', bold=True) document = odf_new_document_from_type('spreadsheet') document.insert_style(header_style, automatic=True) body = document.get_body() table = odf_create_table(u'Table') # Add the header row = odf_create_row() line = self.get_header(resource, context) row.set_values(line, style=header_style) table.append_row(row) # Fill content for item_brain in results.get_documents(): item_resource = root.get_resource(item_brain.abspath) line = self.get_row(resource, context, item_resource) row = odf_create_row() try: row.set_values(line) except Exception: context.message = ERROR(u'Error on line %s' % line) return table.append_row(row) body.append(table) # Extport as ODS f = StringIO() document.save(f) content = f.getvalue() f.close() # Return ODS context.set_content_type( 'application/vnd.oasis.opendocument.spreadsheet') context.set_content_disposition('attachment', self.get_filename(resource)) return content
def test_rstrip(self): row = odf_create_row(width=100) row.set_cell_value(0, 1) row.set_cell_value(1, 2) row.set_cell_value(2, 3) row.set_cell(3, odf_create_cell(style=u"ce1")) row.rstrip_row() self.assertEqual(row.get_row_width(), 4)
def test_rstrip(self): row = odf_create_row(width=100) row.set_value(0, 1) row.set_value(1, 2) row.set_value(2, 3) row.set_cell(3, odf_create_cell(style=u"ce1")) row.rstrip() self.assertEqual(row.get_width(), 4)
def add_row(self, values, is_header=False): if is_header is True: style = self.header_style else: style = None row = odf_create_row() row.set_values(values, style=style) self.table.append_row(row)
def action_export(self, resource, context, form): root = context.root results = context.root.search(format=self.export_resource.class_id) if not len(results): context.message = ERR_NO_DATA return context.query['batch_start'] = context.query['batch_size'] = 0 # Create ODS header_style = odf_create_style('table-cell', area='text', bold=True) document = odf_new_document_from_type('spreadsheet') document.insert_style(header_style, automatic=True) body = document.get_body() table = odf_create_table(u'Table') # Add the header row = odf_create_row() line = self.get_header(resource, context) row.set_values(line, style=header_style) table.append_row(row) # Fill content for item_brain in results.get_documents(): item_resource = root.get_resource(item_brain.abspath) line = self.get_row(resource, context, item_resource) row = odf_create_row() try: row.set_values(line) except Exception: context.message = ERROR(u'Error on line %s' % line) return table.append_row(row) body.append(table) # Extport as ODS f = StringIO() document.save(f) content = f.getvalue() f.close() # Return ODS context.set_content_type('application/vnd.oasis.opendocument.spreadsheet') context.set_content_disposition('attachment', self.get_filename(resource)) return content
def add_row(cls, row, is_header=False): if is_header is True: style = cls.header_style else: style = None values = [] for i, value in enumerate(row): column = cls.columns[i] values.append(column.encode(value)) row = odf_create_row() row.set_values(values, style=style) cls.table.append_row(row)
def create_table(document, data, headers=None, style=None): table = odf_create_table(u"Table") if headers: odt_row = odf_create_row() odt_row.set_values(headers) table.set_row(0, odt_row) for indx, row in enumerate(data): odt_row = odf_create_row() values = [] for val in row: if not val: values.append(u"") continue if isinstance(val, (datetime.date, datetime.datetime)): values.append(val.strftime('%Y %b %d')) continue if isinstance(val, (basestring, unicode, int, float)): values.append(val) continue # In assessment summary table score is a tuple (conclusion, color) if isinstance(val, tuple): values.append(val[0]) continue # ItemList type values.append(', '.join(val.rows)) odt_row.set_values(values) table.set_row(indx + 1, odt_row) apply_table_cell_base_style(document, table) return table
def test_append_rows(D): print "Test append_row", D.lines, "rows", D.cols, "cols" table = odf_create_table(u"Table") C = chrono() for line in xrange(D.lines): row = odf_create_row() row.set_values(D.py_table0[line]) table.append_row(row) C.delta() print "Size of table :", table.get_size() if DEBUG: print table.to_csv() print "-" * 50
def test_append_rows(D): print("Test append_row", D.lines, "rows", D.cols, "cols") table = odf_create_table("Table") C = chrono() for line in range(D.lines): row = odf_create_row() row.set_values(D.py_table0[line]) table.append_row(row) C.delta() print("Size of table :", table.get_size()) if DEBUG: print(table.to_csv()) print("-" * 50)
def update_results(csv_path, lang, results): csv_file = open(csv_path, 'r') lnbr = 0 lang_res = [] while 1: line = csv_file.readline() if line == "": break lnbr = lnbr + 1 if lnbr < 3: continue row = odf_create_row() vals = line.split(",") lang_res.append(float(vals[1])) results[lang] = lang_res
def test_repeated(D): print("test random repeated lines", D.lines, "rows", D.cols, "cols") table = odf_create_table("Table") C = chrono() for line in range(D.lines): row = odf_create_row() row.set_values([(line * 10 + x) for x in range(D.cols)]) row.set_repeated(line) #if DEBUG: # print D.rnd_line[line], "=>", row.get_values(), row.get_repeated() table.set_row(D.rnd_line[line], row) C.delta() print("Size of table :", table.get_size()) if DEBUG: print(table.to_csv()) print("-" * 50) return table
def test_repeated(D): print "test random repeated lines", D.lines, "rows", D.cols, "cols" table = odf_create_table(u"Table") C = chrono() for line in xrange(D.lines): row = odf_create_row() row.set_values([(line * 10 + x) for x in range(D.cols)]) row.set_repeated(line) #if DEBUG: # print D.rnd_line[line], "=>", row.get_values(), row.get_repeated() table.set_row(D.rnd_line[line], row) C.delta() print "Size of table :", table.get_size() if DEBUG: print table.to_csv() print "-" * 50 return table
def spreadsheet_to_text(indoc, outdoc): inbody = indoc.get_body() outbody = outdoc.get_body() # Copy tables for intable in inbody.get_tables(): # clone/rstrip the table clone = intable.clone() clone.rstrip(aggressive=True) # Skip empty table if clone.get_size() == (0, 0): continue # At least OOo Writer doesn't like formulas referencing merged # cells, so expand outtable = odf_create_table(clone.get_name(), style=clone.get_style()) # Columns for column in clone.traverse_columns(): outtable.append(column) # Rows for inrow in clone.traverse(): outrow = odf_create_row(style=inrow.get_style()) # Cells for cell in inrow.traverse(): # Formula formula = cell.get_formula() if formula is not None: if formula.startswith('oooc:'): formula = oooc_to_ooow(formula) else: # Found an OpenFormula test case raise NotImplementedError, formula cell.set_formula(formula) outrow.append(cell) outtable.append_row(outrow) outbody.append(outtable) # Separate tables with an empty line outbody.append(odf_create_paragraph()) # Copy styles for family in ('table', 'table-column', 'table-row', 'table-cell'): for style in indoc.get_styles(family=family): automatic = (style.get_parent().get_tag() == 'office:automatic-styles') default = style.get_tag() == 'style:default-style' outdoc.insert_style(style, automatic=automatic, default=default)
def test_set_rows(D): print("Test random set_row", D.lines, "rows", D.cols, "cols") table = odf_create_table("Table") C = chrono() for line in range(D.lines): row = odf_create_row() row.set_values(D.py_table0[line]) if DEBUG: print(D.rnd_line[line], "=>", D.py_table0[line]) table.set_row(D.rnd_line[line], row) if DEBUG: print(table.to_csv()) C.delta() print("Size of table :", table.get_size()) if DEBUG: print(table.to_csv()) print("-" * 50) return table
def test_set_rows(D): print "Test random set_row", D.lines, "rows", D.cols, "cols" table = odf_create_table(u"Table") C = chrono() for line in xrange(D.lines): row = odf_create_row() row.set_values(D.py_table0[line]) if DEBUG: print D.rnd_line[line], "=>", D.py_table0[line] table.set_row(D.rnd_line[line], row) if DEBUG: print table.to_csv() C.delta() print "Size of table :", table.get_size() if DEBUG: print table.to_csv() print "-" * 50 return table
def _convert_table_rows(container, node, context, cell_style=None): for row in node: if row.tagname != "row": warn('node "%s" not supported in thead/tbody' % row.tagname) continue odf_row = odf_create_row() container.append_element(odf_row) for entry in row: if entry.tagname != "entry": warn('node "%s" not supported in row' % entry.tagname) continue # Create a new odf_cell odf_cell = odf_create_cell(cell_type="string", style=cell_style) odf_row.append_element(odf_cell) # XXX We don't add table:covered-table-cell ! # It's bad but OO can nevertheless load the file morecols = entry.get("morecols") if morecols is not None: morecols = int(morecols) + 1 odf_cell.set_attribute('table:number-columns-spanned', str(morecols)) morerows = entry.get("morerows") if morerows is not None: morerows = int(morerows) + 1 odf_cell.set_attribute('table:number-rows-spanned', str(morerows)) # Save the current top old_top = context["top"] # Convert context["top"] = odf_cell for child in entry: convert_node(child, context) # And restore the top context["top"] = old_top
def test_is_empty(self): row = odf_create_row(width=100) self.assertEqual(row.is_empty(), True)
def test_is_empty_no(self): row = odf_create_row(width=100) row.set_value(50, 1) self.assertEqual(row.is_empty(), False)
def test_style(self): row = odf_create_row(style=u"ro1") expected = '<table:table-row table:style-name="ro1"/>' self.assertEqual(row.serialize(), expected)
def test_set_row_smaller(self): table = self.table.clone() table.set_row(0, odf_create_row(width=table.get_width() - 1)) self.assertEqual(table.get_height(), 4)
def test_insert_row_bigger(self): table = self.table.clone() big_row = odf_create_row(width=table.get_width() + 1) table.insert_row(0, big_row) self.assertEqual(table.get_height(), 5)
def test_set_row_bigger(self): table = self.table.clone() table.set_row(0, odf_create_row(width=table.get_table_width() + 1)) self.assertEqual(table.get_table_height(), 4)
def test_set_row_bigger(self): table = self.table.clone() table.set_row(0, odf_create_row(width=table.get_width() + 1)) self.assertEqual(table.get_height(), 4)
def test_append_row_bigger(self): table = self.table.clone() table.append_row(odf_create_row(width=table.get_width() + 1)) self.assertEqual(table.get_height(), 5)
from lpod.style import make_table_cell_border_string from lpod.style import odf_create_style, rgb2hex # Hello messages print 'lpod installation test' print ' Version : %s' % __version__ print ' Installation path : %s' % __installation_path__ print print 'Generating color chart in my_color_chart.ods ...' document = odf_new_document('spreadsheet') body = document.get_body() table = odf_create_table(u'chart') for y in xrange(0, 256, 8): row = odf_create_row() for x in xrange(0, 256, 32): cell_value = (x, y, (x + y) % 256) border_rl = make_table_cell_border_string(thick='0.20cm', color='white') border_bt = make_table_cell_border_string( thick='0.80cm', color='white', ) style = odf_create_table_cell_style( color='grey', background_color=cell_value, border_right=border_rl, border_left=border_rl, border_bottom=border_bt, border_top=border_bt,
from lpod.style import odf_create_style, rgb2hex # Hello messages print 'lpod installation test' print ' Version : %s' % __version__ print ' Installation path : %s' % __installation_path__ print print 'Generating color chart in my_color_chart.ods ...' document = odf_new_document('spreadsheet') body = document.get_body() table = odf_create_table(u'chart') for y in xrange(0, 256, 8): row = odf_create_row() for x in xrange(0, 256, 32): cell_value = (x, y, (x+y) % 256 ) border_rl = make_table_cell_border_string( thick = '0.20cm', color = 'white' ) border_bt = make_table_cell_border_string( thick = '0.80cm', color = 'white', ) style = odf_create_table_cell_style( color = 'grey' , background_color = cell_value, border_right = border_rl, border_left = border_rl,
def test_insert_row_smaller(self): table = self.table.clone() small_row = odf_create_row(width=table.get_width() - 1) table.insert_row(0, small_row) self.assertEqual(table.get_height(), 5)
def test_is_empty(self): row = odf_create_row(width=100) self.assertEqual(row.is_row_empty(), True)
def test_insert_row_bigger(self): table = self.table.clone() big_row = odf_create_row(width=table.get_table_width() + 1) table.insert_row(0, big_row) self.assertEqual(table.get_table_height(), 5)
def test_is_empty_no(self): row = odf_create_row(width=100) row.set_cell_value(50, 1) self.assertEqual(row.is_row_empty(), False)
def create_table_descr(document, article_data): def set_table_cell_style(_row, color_val): for indx, cell in enumerate(_row.traverse()): if indx == 0: cell.set_style(STYLES[TABLE_CELL_BASE]) else: colored_style = "{}{}".format( TABLE_CELL_AS_VALUE, color_val ) cell.set_style(STYLES[colored_style]) _row.set_cell(x=cell.x, cell=cell) table = odf_create_table(u"Table") row = odf_create_row() row.set_values([ u"Assessment summary: {}".format( article_data.assessment_summary or '-' ), u"Adequacy: {}".format(article_data.adequacy[0]) ]) set_table_cell_style(row, article_data.adequacy[1]) table.set_row(0, row) row = odf_create_row() row.set_values([ u"", u"Consistency: {}".format(article_data.consistency[0]) ]) set_table_cell_style(row, article_data.consistency[1]) table.set_row(1, row) row = odf_create_row() row.set_values([ u"Progress since 2012: {}".format( article_data.progress_assessment or '-' ), u"Coherence: {}".format(article_data.coherence[0]) ]) set_table_cell_style(row, article_data.coherence[1]) table.set_row(2, row) row = odf_create_row() row.set_values([ u"", u"Overall score 2018: {}".format(article_data.overall_score_2018[0]) ]) set_table_cell_style(row, article_data.overall_score_2018[1]) table.set_row(3, row) row = odf_create_row() row.set_values([ u"Recommendations: {}".format(article_data.recommendations or '-'), u"Adequacy score 2012: {}".format(article_data.overall_score_2012[0]) ]) set_table_cell_style(row, article_data.overall_score_2012[1]) table.set_row(4, row) row = odf_create_row() row.set_values([ u"", u"Adequacy change since 2012: {}".format(article_data.change_since_2012) ]) set_table_cell_style(row, 0) table.set_row(5, row) table.set_span((0, 0, 0, 1)) table.set_span((0, 2, 0, 3)) table.set_span((0, 4, 0, 5)) # apply_table_cell_base_style(document, table) apply_descriptors_table_style(document, table) return table
def test_repeated(self): row = odf_create_row(repeated=3) expected = '<table:table-row table:number-rows-repeated="3"/>' self.assertEqual(row.serialize(), expected)
return n / 2 return 3 * n + 1 if __name__=="__main__": spreadsheet = odf_new_document('spreadsheet') # Populate the table in the spreadsheet body = spreadsheet.get_body() table = odf_create_table(u"Big Table") body.append(table) lines = 1000 cols = 100 for line in range(lines): row = odf_create_row() values = [] n = line for i in range(cols): values.append(n) n = suite(n) row.set_values(values) table.append(row) print "Size of Big Table :", table.get_size() # now extract 100 rows of 26 columns : table1 = odf_create_table(u"Extract 1") for r in range(800,900): row = table.get_row(r) values = [ row.get_value(x) for x in xrange(50,76) ]
def test_insert_row_smaller(self): table = self.table.clone() small_row = odf_create_row(width=table.get_table_width() - 1) table.insert_row(0, small_row) self.assertEqual(table.get_table_height(), 5)
def test_append_row_bigger(self): table = self.table.clone() table.append_row(odf_create_row(width=table.get_table_width() + 1)) self.assertEqual(table.get_table_height(), 5)
def test_width(self): row = odf_create_row(1) expected = '<table:table-row><table:table-cell/></table:table-row>' self.assertEqual(row.serialize(), expected)
def test_default(self): row = odf_create_row() expected = '<table:table-row/>' self.assertEqual(row.serialize(), expected)
def test_append_row_smaller(self): table = self.table.clone() table.append_row(odf_create_row(width=table.get_width() - 1)) self.assertEqual(table.get_height(), 5)