def divide_range(cell_range, by="columns"): """ Returns a list of cell range (string) divided in columns or rows cell_range -- str like Sheet.A1:Sheet.C5 by -- 'columns' 'rows' """ my_list = [] sheet1=splitfields(splitfields(cell_range, ":")[0],".")[0] cell1=splitfields(splitfields(cell_range, ":")[0],".")[1] sheet2=splitfields(splitfields(cell_range, ":")[1],".")[0] cell2=splitfields(splitfields(cell_range, ":")[1],".")[1] tmp_coord1 = _get_cell_coordinates(cell1) tmp_coord2 = _get_cell_coordinates(cell2) #we need the coordinate in a crescent order coord1 = (min(tmp_coord1[0], tmp_coord2[0]),min(tmp_coord1[1], tmp_coord2[1])) coord2 = (max(tmp_coord1[0], tmp_coord2[0]),max(tmp_coord1[1], tmp_coord2[1])) if by == 'columns': for i in range(coord1[0], coord2[0]+1): tmp_cell_range = sheet1 + "." + _digit_to_alpha(i) + \ str(coord1[1]+1) + ":" + sheet2 + "." + \ _digit_to_alpha(i) + str(coord2[1]+1) my_list.append(tmp_cell_range) return my_list elif by == 'rows': for i in range(coord1[1], coord2[1]+1): tmp_cell_range = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(i+1) + ":" + sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(i+1) \ my_list.append(tmp_cell_range) return my_list else: raise AttributeError
def divide_range(cell_range, by="columns"): """ Returns a list of cell range (string) divided in columns or rows cell_range -- str like Sheet.A1:Sheet.C5 by -- 'columns' 'rows' """ my_list = [] sheet1 = splitfields(splitfields(cell_range, ":")[0], ".")[0] cell1 = splitfields(splitfields(cell_range, ":")[0], ".")[1] sheet2 = splitfields(splitfields(cell_range, ":")[1], ".")[0] cell2 = splitfields(splitfields(cell_range, ":")[1], ".")[1] tmp_coord1 = _get_cell_coordinates(cell1) tmp_coord2 = _get_cell_coordinates(cell2) #we need the coordinate in a crescent order coord1 = (min(tmp_coord1[0], tmp_coord2[0]), min(tmp_coord1[1], tmp_coord2[1])) coord2 = (max(tmp_coord1[0], tmp_coord2[0]), max(tmp_coord1[1], tmp_coord2[1])) if by == 'columns': for i in range(coord1[0], coord2[0] + 1): tmp_cell_range = sheet1 + "." + _digit_to_alpha(i) + \ str(coord1[1]+1) + ":" + sheet2 + "." + \ _digit_to_alpha(i) + str(coord2[1]+1) my_list.append(tmp_cell_range) return my_list elif by == 'rows': for i in range(coord1[1], coord2[1] + 1): tmp_cell_range = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(i+1) + ":" + sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(i+1) \ my_list.append(tmp_cell_range) return my_list else: raise AttributeError
def split_range(cell_range, legend="row", x_labels="column"): """ returns a dictionnary with data_range, legend_range and labels for the x-axis cell_range - str like Sheet.A1:SheetD5 legend, x_labels - 'none', 'row', 'column' """ my_dict = {"data":cell_range, "legend":"", "labels":""} sheet1=splitfields(splitfields(cell_range, ":")[0],".")[0] cell1=splitfields(splitfields(cell_range, ":")[0],".")[1] sheet2=splitfields(splitfields(cell_range, ":")[1],".")[0] cell2=splitfields(splitfields(cell_range, ":")[1],".")[1] tmp_coord1 = _get_cell_coordinates(cell1) tmp_coord2 = _get_cell_coordinates(cell2) #we need the coordinate in a crescent order coord1 = (min(tmp_coord1[0], tmp_coord2[0]),min(tmp_coord1[1], tmp_coord2[1])) coord2 = (max(tmp_coord1[0], tmp_coord2[0]),max(tmp_coord1[1], tmp_coord2[1])) if legend is not 'none' and x_labels is not 'none': #if we have both conditions, we have to delete the first cell if legend == "row": my_dict["legend"] = sheet1 + "." + _digit_to_alpha(coord1[0]+1) + \ str(coord1[1]+1) + ":"+sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(coord1[1]+1) elif legend == "column": my_dict["legend"] = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(coord1[1]+2) + ":"+sheet2 + "." + \ _digit_to_alpha(coord1[0]) + str(coord2[1]+1) if x_labels == "row": my_dict["labels"] = sheet1 + "." + _digit_to_alpha(coord1[0]+1) + \ str(coord1[1]+1) + ":"+sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(coord1[1]+1) elif x_labels == "column": my_dict["labels"] = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(coord1[1]+2) + ":"+sheet2 + "." + \ _digit_to_alpha(coord1[0]) + str(coord2[1]+1) my_dict["data"] = sheet1 + "." + _digit_to_alpha(coord1[0]+1) + \ str(coord1[1]+2) + ":"+sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(coord2[1]+1) else: if legend == "row": my_dict["legend"] = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(coord1[1]+1) + ":"+sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(coord1[1]+1) my_dict["data"] = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(coord1[1]+2) + ":" + sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(coord2[1]+1) elif legend == "column": my_dict["legend"] = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(coord1[1]+1) + ":"+sheet2 + "." + \ _digit_to_alpha(coord1[0]) + str(coord2[1]+1) my_dict["data"] = sheet1 + "." + _digit_to_alpha(coord1[0]+1) + \ str(coord1[1]+1) + ":"+sheet2 + \ "."+_digit_to_alpha(coord2[0]) + str(coord2[1]+1) if x_labels == "row": my_dict["labels"] = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(coord1[1]+1) + ":" + sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(coord1[1]+1) my_dict["data"] = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(coord1[1]+2) + ":"+sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(coord2[1]+1) elif x_labels == "column": my_dict["labels"] = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(coord1[1]+1) + ":"+sheet2 + "." + \ _digit_to_alpha(coord1[0]) + str(coord2[1]+1) my_dict["data"] = sheet1 + "."+ _digit_to_alpha(coord1[0]+1) + \ str(coord1[1]+1) + ":"+sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(coord2[1]+1) return my_dict
def test_digit_to_alpha_digit(self): self.assertEqual(_digit_to_alpha('ABC'), 'ABC')
def test_digit_to_alpha_to_digit(self): for i in range(1024): self.assertEqual(_alpha_to_digit(_digit_to_alpha(i)), i)
for i in range(len(result[0])): row = odf_create_row() cell = odf_create_cell("Place "+str(places[i])) row.append_cell(cell) for j in range(len(result)): cell = odf_create_cell(result[j][i]) row.append_cell(cell) table.append_row(row) ### #Third step : create our chart ### table = odf_create_table('Gantt') body.append(table) cell_range="Data.A1:Data."+_digit_to_alpha(len(result))+str(len(result[0])) cols_list = divide_range(cell_range) #we add the xml files that will contain the chart informations chart_directory = add_chart_structure_in_document(document) #we need an access to the office:automatic-styles element chart_content = odf_xmlpart(chart_directory+'/content.xml', document) styles = chart_content.get_element("office:automatic-styles") #we add informations about style styles.append(odf_create_chart_title_style("titre", "12pt")) plot_s = odf_create_chart_plot_area_style("plot") plot_s.set_vertical("true") plot_s.set_stacked("true")
def split_range(cell_range, legend="row", x_labels="column"): """ returns a dictionnary with data_range, legend_range and labels for the x-axis cell_range - str like Sheet.A1:SheetD5 legend, x_labels - 'none', 'row', 'column' """ my_dict = {"data": cell_range, "legend": "", "labels": ""} sheet1 = splitfields(splitfields(cell_range, ":")[0], ".")[0] cell1 = splitfields(splitfields(cell_range, ":")[0], ".")[1] sheet2 = splitfields(splitfields(cell_range, ":")[1], ".")[0] cell2 = splitfields(splitfields(cell_range, ":")[1], ".")[1] tmp_coord1 = _get_cell_coordinates(cell1) tmp_coord2 = _get_cell_coordinates(cell2) #we need the coordinate in a crescent order coord1 = (min(tmp_coord1[0], tmp_coord2[0]), min(tmp_coord1[1], tmp_coord2[1])) coord2 = (max(tmp_coord1[0], tmp_coord2[0]), max(tmp_coord1[1], tmp_coord2[1])) if legend is not 'none' and x_labels is not 'none': #if we have both conditions, we have to delete the first cell if legend == "row": my_dict["legend"] = sheet1 + "." + _digit_to_alpha(coord1[0]+1) + \ str(coord1[1]+1) + ":"+sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(coord1[1]+1) elif legend == "column": my_dict["legend"] = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(coord1[1]+2) + ":"+sheet2 + "." + \ _digit_to_alpha(coord1[0]) + str(coord2[1]+1) if x_labels == "row": my_dict["labels"] = sheet1 + "." + _digit_to_alpha(coord1[0]+1) + \ str(coord1[1]+1) + ":"+sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(coord1[1]+1) elif x_labels == "column": my_dict["labels"] = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(coord1[1]+2) + ":"+sheet2 + "." + \ _digit_to_alpha(coord1[0]) + str(coord2[1]+1) my_dict["data"] = sheet1 + "." + _digit_to_alpha(coord1[0]+1) + \ str(coord1[1]+2) + ":"+sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(coord2[1]+1) else: if legend == "row": my_dict["legend"] = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(coord1[1]+1) + ":"+sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(coord1[1]+1) my_dict["data"] = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(coord1[1]+2) + ":" + sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(coord2[1]+1) elif legend == "column": my_dict["legend"] = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(coord1[1]+1) + ":"+sheet2 + "." + \ _digit_to_alpha(coord1[0]) + str(coord2[1]+1) my_dict["data"] = sheet1 + "." + _digit_to_alpha(coord1[0]+1) + \ str(coord1[1]+1) + ":"+sheet2 + \ "."+_digit_to_alpha(coord2[0]) + str(coord2[1]+1) if x_labels == "row": my_dict["labels"] = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(coord1[1]+1) + ":" + sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(coord1[1]+1) my_dict["data"] = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(coord1[1]+2) + ":"+sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(coord2[1]+1) elif x_labels == "column": my_dict["labels"] = sheet1 + "." + _digit_to_alpha(coord1[0]) + \ str(coord1[1]+1) + ":"+sheet2 + "." + \ _digit_to_alpha(coord1[0]) + str(coord2[1]+1) my_dict["data"] = sheet1 + "."+ _digit_to_alpha(coord1[0]+1) + \ str(coord1[1]+1) + ":"+sheet2 + "." + \ _digit_to_alpha(coord2[0]) + str(coord2[1]+1) return my_dict