def get_material_sheets(glider): # ribs ribs_sheet = ezodf.Table(name="material_ribs", size=(len(glider.ribs) + 2, 2)) ribs_sheet[0, 0].set_value("Rib") ribs_sheet[0, 1].set_value("Material") for rib_no, rib in enumerate(glider.ribs): ribs_sheet[rib_no + 1, 0].set_value(rib.name) ribs_sheet[rib_no + 1, 1].set_value(rib.material_code) # panels all_panels = sum([cell.panels for cell in glider.cells], []) panel_sheet = ezodf.Table(name="material_panels", size=(len(all_panels) + 1, 2)) panel_sheet[0, 0].set_value("Panel") panel_sheet[0, 1].set_value("Material") for panel_no, panel in enumerate(all_panels): panel_sheet[panel_no + 1, 0].set_value(panel.name) panel_sheet[panel_no + 1, 1].set_value(panel.material_code) # dribs all_dribs = sum([cell.diagonals for cell in glider.cells], []) dribs_sheet = ezodf.Table(name="material_dribs", size=(len(all_dribs) + 1, 2)) dribs_sheet[0, 0].set_value("Diagonal") dribs_sheet[0, 1].set_value("Material") for drib_no, drib in enumerate(all_dribs): dribs_sheet[drib_no + 1, 0].set_value(drib.name) dribs_sheet[drib_no + 1, 1].set_value(drib.material_code) return ribs_sheet, panel_sheet, dribs_sheet
def get_lines_sheet(glider, places=3): table = glider.lineset.get_input_table() ods_sheet = table.get_ods_sheet("Lines") return ods_sheet lower_nodes = glider.lineset.get_lower_attachment_points() line_trees = [glider.lineset.create_tree(node) for node in lower_nodes] ods_sheet = ezodf.Table(name="Lines", size=(500, 500)) def insert_block(line, upper, row, column): ods_sheet[row, column + 1].set_value(line.line_type.name) if upper: ods_sheet[row, column].set_value(line.target_length) for line, line_upper in upper: row = insert_block(line, line_upper, row, column + 2) else: # Insert a top node name = line.upper_node.name if not name: name = "Rib_{}/{}".format(line.upper_node.rib_no, line.upper_node.rib_pos) ods_sheet[row, column].set_value(name) row += 1 return row row = 0 for node_no, tree in enumerate(line_trees): ods_sheet[row, 0].set_value(node_no) for line, upper in tree: row = insert_block(line, upper, row, 1) return ods_sheet
def create(self, path): self._path = path self._prepend = None self._workbook = ezodf.newdoc(doctype='ods', filename=path) self._worksheet = ezodf.Table(self.settings.worksheet, size=(self.end['row'], self.end['col'])) self._workbook.sheets.append(self._worksheet)
def _handle_evaluation_request(request, items, context=None): # context is always None if comming from Ringo. clazz = request.context.__model__ modul = request.context.__modul__ renderer = EvaluationDialogRenderer(request, clazz) form = renderer.form if (request.method == 'POST' and is_confirmed(request) and form.validate(request.params)): # 1. Load evaluation file factory = Extension.get_item_factory() evaluation = factory.load(form.data["evaluations"]) exportformat = form.data.get("exportformat", "ods") export_config = evaluation.config_as_json spreadsheet = ezodf.opendoc(load_file(evaluation.data)) # 2. Export data exporter = RecursiveRelationExporter(clazz, export_config) sheet_data = exporter.perform(items) relation_config = exporter.get_relation_config() # 3. Create sheets per relation sheets = spreadsheet.sheets for relation in relation_config: data = sheet_data[relation] if relation == "root": sheetname = modul.name else: sheetname = relation try: sheet = sheets[sheetname] except: sheets.append(ezodf.Table(sheetname)) sheet = sheets[sheetname] _fill_sheet(sheet, data, relation_config[relation]) # 4. Convert and recalcualte the spreadsheet # converter = get_converter() # if converter.is_available(): # spreadsheet_data = _convert_spreadsheet(spreadsheet, exportformat) # else: spreadsheet_data = spreadsheet.tobytes() # 5. Build response resp = request.response resp.content_type = str('application/%s' % exportformat) ctime = get_local_datetime(datetime.utcnow()) filename = "%s_%s.%s" % (ctime.strftime("%Y-%m-%d-%H%M"), sheetname.lower(), exportformat) resp.content_disposition = 'attachment; filename=%s' % filename resp.body = spreadsheet_data return resp else: # FIXME: Get the ActionItem here and provide this in the Dialog to get # the translation working (torsten) <2013-07-10 09:32> rvalue = {} rvalue['dialog'] = renderer.render(items) return rvalue
def create(self, path): self._path = path self._max_cells = max(self.cells) self._workbook = ezodf.newdoc(doctype='ods', filename=path) self._worksheet = ezodf.Table(self.settings.worksheet, size=(self.end['row'], self._max_cells + 1)) self._workbook.sheets.append(self._worksheet)
def get_ods_sheet(self, name=None): ods_sheet = ezodf.Table(size=(self.num_rows, self.num_columns)) for key in self.dct: column, row = self.str_decrypt(key) ods_sheet[row, column].set_value(self.dct[key]) if name: ods_sheet.name = name return ods_sheet
def transpose_columns(sheet=ezodf.Table(), columnswidth=2): num = sheet.ncols() #if num % columnswidth > 0: # raise ValueError("irregular columnswidth") result = [] for col in range(int(num / columnswidth)): columns = range(col * columnswidth, (col + 1) * columnswidth) element = [] i = 0 while i < sheet.nrows(): row = [sheet.get_cell([i, j]).value for j in columns] if sum([j is None for j in row]) == len(row): # Break at empty line break i += 1 element.append(row) result.append(element) return result
def get_length_table(glider): table = ezodf.Table(name="straps", size=(500, 500)) num = 0 for cell_no, cell in enumerate(glider.cells): num = max(num, len(cell.straps)) table[cell_no + 1, 0].set_value("cell_{}".format(cell_no)) for strap_no, strap in enumerate(cell.straps): table[cell_no + 1, 2 * strap_no + 1].set_value("{}/{}".format( strap.center_left, strap.center_right)) table[cell_no + 1, 2 * strap_no + 2].set_value(strap.get_center_length(cell)) for i in range(num): table[0, 2 * i + 1].set_value("pos") table[0, 2 * i + 2].set_value("length") return table
def get_length_table(glider): table = ezodf.Table(name="rigidfoils", size=(500, 500)) num = 0 for rib_no, rib in enumerate(glider.ribs): num = max(num, len(rib.rigidfoils)) table[rib_no + 1, 0].set_value("Rib_{}".format(rib_no)) for rigid_no, rigidfoil in enumerate(rib.rigidfoils): table[rib_no + 1, 2 * rigid_no + 1].set_value("{}/{}".format( rigidfoil.start, rigidfoil.end)) table[rib_no + 1, 2 * rigid_no + 2].set_value(rigidfoil.get_length(rib)) for i in range(num): table[0, 2 * i + 1].set_value("start/stop") table[0, 2 * i + 2].set_value("length") return table
def exportTable(matrix, fname, ext, title=None): """Save data to a file Inputs matrix: array with data to save fname: name of file to save ext: name of format to save csv | ods | xls | xlsx title: column title array, optional """ sheetTitle = QApplication.translate("pychemqt", "Table") if fname.split(".")[-1] != ext: fname += ".%s" % ext # Format title header = [] if title: for ttl in title: line = str(ttl).split(os.linesep) if line[-1] != "[-]": line[-1] = "[" + line[-1] + "]" header.append(" ".join(line)) if ext == "csv": import csv with open(fname, 'w') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=header) writer.writeheader() for row in matrix: kw = {} for ttl, value in zip(header, row): kw[ttl] = value writer.writerow(kw) elif ext == "ods": import ezodf spreadsheet = ezodf.newdoc("ods", fname) sheets = spreadsheet.sheets sheet = ezodf.Table(sheetTitle) sheets += sheet sheet.reset(size=(len(matrix) + 1, len(matrix[0]))) # Add Data if title: for i, ttl in enumerate(header): sheet["%s%i" % (spreadsheetColumn(i), 1)].set_value(ttl) for j, row in enumerate(matrix): for i, data in enumerate(row): sheet["%s%i" % (spreadsheetColumn(i), j + 2)].set_value(data) spreadsheet.save() elif ext == "xls": import xlwt spreadsheet = xlwt.Workbook() sheet = spreadsheet.add_sheet(sheetTitle) font = xlwt.Font() font.bold = True style = xlwt.XFStyle() style.font = font # Add Data if title: for i, ttl in enumerate(header): sheet.write(0, i, ttl, style) for j, row in enumerate(matrix): for i, data in enumerate(row): sheet.write(j + 1, i, data) spreadsheet.save(fname) elif ext == "xlsx": import openpyxl from openpyxl.styles import Font spreadsheet = openpyxl.Workbook() sheet = spreadsheet.active sheet.title = sheetTitle font1 = Font() font1.size = 9 font1.bold = True font2 = Font() font2.size = 9 # Add Data if title: for i, ttl in enumerate(header): sheet["%s%i" % (spreadsheetColumn(i), 1)] = ttl sheet["%s%i" % (spreadsheetColumn(i), 1)].style.font = font1 for j, row in enumerate(matrix): for i, data in enumerate(row): sheet["%s%i" % (spreadsheetColumn(i), j + 2)] = data sheet["%s%i" % (spreadsheetColumn(i), j + 2)].style.font = font2 spreadsheet.save(filename=fname) else: raise ValueError( QApplication.translate("pychemqt", "Unsopported format") + " " + ext)
#coding:utf-8 # Purpose: swap row/columns of a table # Created: 04.10.2011 # Copyright (C) 2011, Manfred Moitzi # License: MIT license from __future__ import unicode_literals, print_function, division __author__ = "mozman <*****@*****.**>" import ezodf ROWS, COLS = 20, 10 # create a new spreadsheet document doc = ezodf.newdoc(doctype="ods", filename="my_spreadsheet.ods") # create a new table table = ezodf.Table(name="Table", size=(ROWS, COLS)) # add table to document doc.sheets += table # set table values for row in range(ROWS): for col in range(COLS): content = "%s%s" % (chr(65 + col), row + 1) cell = table[row, col] cell.set_value(content) # create the symmetric table symtable = ezodf.Table(name="Symmetry", size=(COLS, ROWS)) doc.sheets += symtable # set symmetric values
def exportTable(matrix, fname, format, title=None): """Save data to a file Inputs matrix: array with data to save fname: name of file to save format: name of format to save csv | ods | xls | xlsx title: column title array, optional """ sheetTitle = unicode(QApplication.translate("pychemqt", "Table")) if fname.split(".")[-1] != format: fname += ".%s" % format # Format title if title: header = [] for ttl in title: line = unicode(ttl).split(os.linesep) if line[-1] != "[-]": line[-1] = "[" + line[-1] + "]" header.append(" ".join(line)) c_newline = maketrans(os.linesep, " ") if format == "csv": import csv with open(fname, "w") as archivo: writer = csv.writer(archivo, delimiter='\t', quotechar='"', quoting=csv.QUOTE_NONE) # Add Data if title: writer.writerow([ttl.translate(c_newline) for ttl in header]) c_float = maketrans(".", ",") for row in matrix: writer.writerow([str(data).translate(c_float) for data in row]) elif format == "ods": import ezodf spreadsheet = ezodf.newdoc("ods", fname) sheets = spreadsheet.sheets sheet = ezodf.Table(sheetTitle) sheets += sheet sheet.reset(size=(len(matrix) + 1, len(matrix[0]))) # Add Data if title: for i, ttl in enumerate(header): sheet["%s%i" % (spreadsheetColumn(i), 1)].set_value(ttl) for j, row in enumerate(matrix): for i, data in enumerate(row): sheet["%s%i" % (spreadsheetColumn(i), j + 2)].set_value(data) spreadsheet.save() elif format == "xls": import xlwt spreadsheet = xlwt.Workbook() sheet = spreadsheet.add_sheet(sheetTitle) font = xlwt.Font() font.bold = True style = xlwt.XFStyle() style.font = font # Add Data if title: for i, ttl in enumerate(header): sheet.write(0, i, ttl, style) for j, row in enumerate(matrix): for i, data in enumerate(row): sheet.write(j + 1, i, data) spreadsheet.save(fname) elif format == "xlsx": import openpyxl from openpyxl.styles import Style, Font spreadsheet = openpyxl.Workbook() sheet = spreadsheet.active sheet.title = sheetTitle font1 = Font() font1.size = 9 font1.bold = True font2 = Font() font2.size = 9 # Add Data if title: for i, ttl in enumerate(header): sheet["%s%i" % (spreadsheetColumn(i), 1)] = ttl sheet["%s%i" % (spreadsheetColumn(i), 1)].style.font = font1 for j, row in enumerate(matrix): for i, data in enumerate(row): sheet["%s%i" % (spreadsheetColumn(i), j + 2)] = data sheet["%s%i" % (spreadsheetColumn(i), j + 2)].style.font = font2 spreadsheet.save(filename=fname) else: raise ValueError( QApplication.translate("pychemqt", "Unsopported format") + " " + format)