Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
TOuCAN/bin/julianos_xlsxgen_vTOuCAN.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
900 lines (835 sloc)
29.1 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
glob_dictionary = {} | |
glob_workbook = '' | |
glob_sheets = [] | |
glob_cond_formats = {} | |
glob_true = '' | |
glob_false = '' | |
glob_regex = {} | |
glob_listx = [] | |
glob_filecount = 0 | |
glob_float = "" | |
glob_debug = False | |
glob_dict_ROW = {} | |
glob_dict_COL = {} | |
glob_dict_CLL = {} | |
glob_dict_regex = {} | |
glob_dict_color = { "float_format":["#cfc9fc", "light blue violet", "Float num cells with \"0.00\" num_format", "#95b3d7"], | |
"int_format":["#9e98e0", "blue violet", "Int num cells with \"#,###\" num_format", "#63c7ce"], | |
"text_format":["#da9694", "red", "Text cells formatted via a JSON format"], | |
"regex_finding":["#c4d79b", "leaf green", "Cells where a regex was found"], | |
"regex_cond_finding":["#85b782", "moss green", "Cells where a conditional format regex was found"], | |
"num_format":["#79b1e5", "blue", "Num cells formatted via a JSON format"], | |
"text_auto":["#ffe2aa", "peach", "Text cells automatically recognized by the script"]} | |
def add_freeze( sheetx, listx , fileinfo , filecount ): | |
freezerow = False | |
freezecol = False | |
freeze_row = 0 | |
freeze_col = 0 | |
try: | |
freeze_row = fileinfo["freeze_row"] | |
freezerow = True | |
except: | |
pass | |
try: | |
freeze_col = fileinfo["freeze_col"] | |
freezecol = True | |
except: | |
pass | |
if freezerow == True or freezecol == True: | |
sheetx.freeze_panes(freeze_row, freeze_col, freeze_row-1, 0) | |
else: | |
print(" >> no freeze parameters found") | |
def add_filters( sheetx , listx , fileinfo , filecount ): | |
headline = 0 | |
try: | |
print(" -- generate filters") | |
filter_regex = fileinfo["filter_regex"] | |
regex_x = re.compile("("+filter_regex+")") | |
headline = 0 | |
counter = 0 | |
for i in listx: | |
if regex_x.search(i[0]): | |
headline = counter | |
counter = counter + 1 | |
sheetx.autofilter(headline,0,headline,len(listx[headline])) | |
except: | |
print(" >> no filters found --") | |
pass | |
try: | |
print(" -- generate specific filters --") | |
filters = glob_dictionary["files"][filecount]["filters"] | |
filtercount = 0 | |
for i in filters: | |
filter_regex = glob_dictionary["files"][filecount]["filters"][filtercount]["anchor_regex"] | |
condition = glob_dictionary["files"][filecount]["filters"][filtercount]["condition"] | |
compare_valstr = glob_dictionary["files"][filecount]["filters"][filtercount]["compare_value_string"] | |
regex_x = re.compile("("+filter_regex+")") | |
col_count = 0 | |
for x in listx[headline]: | |
if regex_x.search(x): | |
criteria = 'x' + ' ' + str(condition) + ' ' + str(compare_valstr) | |
sheetx.filter_column(col_count, criteria) | |
col_count = col_count + 1 | |
filtercount = filtercount + 1 | |
except: | |
print(" >> no specific filters found") | |
pass | |
def generate_cond_format( fileinfo , filecount ): | |
print(" -- generating conditional formats --") | |
global glob_cond_formats; global glob_true; global glob_false | |
glob_true = glob_workbook.add_format(); glob_true.set_bg_color('#c0f0cb'); glob_true.set_font_color('#006100') | |
glob_false = glob_workbook.add_format(); glob_false.set_bg_color('#ffc5d0'); glob_false.set_font_color('#9c0006') | |
try: | |
cond_formats = fileinfo["conditional_formats"] | |
formatcount = 0 | |
for entry in cond_formats: | |
mode = entry.get("anchor_regex") | |
regex_x = re.compile("("+str(entry.get("anchor_regex"))+")") | |
cond_clearer = "" | |
if entry.get("conditional_mode") == 3 or entry.get("conditional_mode") == "3": | |
cond_clearer = "3_color_scale" | |
elif entry.get("conditional_mode") == 2 or entry.get("conditional_mode") == "3": | |
cond_clearer = "2_color_scale" | |
elif entry.get("conditional_mode") == "tf": | |
cond_clearer = "tf" | |
glob_cond_formats[regex_x] = [formatcount, cond_clearer] | |
if glob_debug == True: | |
demanded_position_list = finding_regexes(regex_x) | |
for match in demanded_position_list: | |
glob_dict_regex[(match[0], match[1])] = 1 | |
formatcount = formatcount + 1 | |
except: | |
print(" >> no conditional formats found") | |
def cond_checker( sheetx , filecount , cond_format_info , row , col , end_row ): | |
cond_no = cond_format_info[0] | |
cond_type = cond_format_info[1] | |
cond_format = glob_dictionary["files"][filecount]["conditional_formats"][cond_no] | |
if cond_type == "3_color_scale": | |
try: | |
min_value = float(cond_format["conditional_numeric_minvalue"]) | |
mid_value = float(cond_format["conditional_numeric_midvalue"]) | |
max_value = float(cond_format["conditional_numeric_maxvalue"]) | |
sheetx.conditional_format(cond_format["start_row"] + row, cond_format["start_col"] + col, end_row - 1, cond_format["end_col"] + col, | |
{ "type":cond_type, | |
'min_color':cond_format["conditional_numeric_mincolor"], | |
'mid_color':cond_format["conditional_numeric_midcolor"], | |
'max_color':cond_format["conditional_numeric_maxcolor"], | |
'min_type':cond_format["conditional_type"], | |
'mid_type':cond_format["conditional_type"], | |
'max_type':cond_format["conditional_type"], | |
'min_value':min_value, | |
'mid_value':mid_value, | |
'max_value':max_value}) | |
except: | |
sheetx.conditional_format(cond_format["start_row"] + row, cond_format["start_col"] + col, end_row - 1, cond_format["end_col"] + col, | |
{ "type":cond_type, | |
"min_color":cond_format["conditional_numeric_mincolor"], | |
"mid_color":cond_format["conditional_numeric_midcolor"], | |
"max_color":cond_format["conditional_numeric_maxcolor"]}) | |
elif cond_type == "2_color_scale": | |
try: | |
min_value = float(cond_format["conditional_numeric_minvalue"]) | |
max_value = float(cond_format["conditional_numeric_maxvalue"]) | |
sheetx.conditional_format(cond_format["start_row"] + row, cond_format["start_col"] + col, end_row - 1, cond_format["end_col"] + col, | |
{ "type":cond_type, | |
'min_color':cond_format["conditional_numeric_mincolor"], | |
'max_color':cond_format["conditional_numeric_maxcolor"], | |
'min_type':cond_format["conditional_type"], | |
'max_type':cond_format["conditional_type"], | |
'min_value':min_value, | |
'max_value':max_value}) | |
except: | |
sheetx.conditional_format(cond_format["start_row"] + row, cond_format["start_col"] + col, end_row - 1, cond_format["end_col"] + col, | |
{ "type":cond_type, | |
"min_color":cond_format["conditional_numeric_mincolor"], | |
"max_color":cond_format["conditional_numeric_maxcolor"]}) | |
elif cond_type == "tf": | |
sheetx.conditional_format(cond_format["start_row"] + row, cond_format["start_col"] + col, end_row - 1, cond_format["end_col"] + col, | |
{ 'type':'text', | |
'criteria':'begins with', | |
'value':'T', | |
'format':glob_true}) | |
sheetx.conditional_format(cond_format["start_row"] + row, cond_format["start_col"] + col, end_row - 1, cond_format["end_col"] + col, | |
{ 'type':'text', | |
'criteria':'begins with', | |
'value':'F', | |
'format':glob_false}) | |
def cond_format_finder_writer( sheetx , listx , fileinfo , filecount ): | |
print(" -- comparing list(s) with conditional regexes -- ") | |
global glob_dict_regex; | |
cond_control = False | |
head_control = False | |
headline = 0 | |
try: | |
cond_path = fileinfo["conditional_formats"] | |
cond_control = True | |
except: | |
pass | |
try: | |
headline_regex = fileinfo["headline_regex"] | |
head_control = True | |
except: | |
pass | |
if cond_control == False: | |
return | |
if head_control == True: | |
row_max = len(listx) | |
end_row = row_max | |
counter = 0 | |
for x in listx: | |
if headline_regex in str(x[0]): | |
headline = counter | |
counter = counter + 1 | |
for col in range(0,len(listx[headline])): | |
for cond_format in glob_cond_formats: | |
if cond_format.search(str(listx[headline][col])): | |
cond_checker(sheetx, filecount, glob_cond_formats[cond_format], headline, col, end_row) | |
else: | |
row_max = len(listx) | |
##### Because no headline: Comparing every cell with every cond_format-regex. | |
for row in range(0, row_max): | |
for col in range(0, len(listx[row])): | |
for cond_format in glob_cond_formats: | |
if cond_format.search(str(listx[row][col])): | |
end_row = 0 | |
for row2 in range(row,row_max): | |
if str(listx[row2][col]) == ' ' or str(listx[row2][col]) == '': | |
end_row = row2 | |
break | |
cond_checker(sheetx, filecount, glob_cond_formats[cond_format], row, col , end_row) | |
def merge_formats( filecount, format_no1 , format_no2 ): | |
workbook = glob_workbook | |
format1 = glob_dictionary["files"][filecount]["basic_formats"][format_no1] | |
format2 = glob_dictionary["files"][filecount]["basic_formats"][format_no2] | |
formatx = workbook.add_format() | |
# print(glob_dictionary["files"][filecount]["basic_formats"][format_no1]) | |
# print(glob_dictionary["files"][filecount]["basic_formats"][format_no2]) | |
if format1.get("bold") == 1 or format2.get("bold") == 1: | |
formatx.set_bold() | |
if format1.get("italic") == 1 or format2.get("italic") == 1: | |
formatx.set_italic() | |
if format1.get("border_bottom") == 1 or format2.get("border_bottom") == 1: | |
formatx.set_bottom() | |
if format1.get("border_right") == 1 or format2.get("border_right") == 1: | |
formatx.set_right() | |
if format1.get("text_wrap") == 1 or format2.get("text_wrap") == 1: | |
formatx.set_text_wrap() | |
if format1.get("anchor_mode") == "row": | |
for order in format1.get("alignments"): | |
formatx.set_align(order) | |
else: | |
for order in format2.get("alignments"): | |
formatx.set_align(order) | |
fill_color1 = None | |
fill_color2 = None | |
try: | |
fill_color1 = format1.get("fill_color") | |
except: | |
pass | |
try: | |
fill_color2 = format2.get("fill_color") | |
except: | |
pass | |
if fill_color1 != None and fill_color2 == None: | |
formatx.set_bg_color(format1.get("fill_color")) | |
elif fill_color1 != None and fill_color2 != None: | |
formatx.set_bg_color(format1.get("fill_color")) | |
elif fill_color1 == None and fill_color2 != None: | |
formatx.set_bg_color(format2.get("fill_color")) | |
if glob_debug == True: | |
formatx.set_bg_color(glob_dict_color["text_format"][0]) | |
font_color1 = None | |
font_color2 = None | |
try: | |
font_color1 = format1.get("font_color") | |
except: | |
pass | |
try: | |
font_color2 = format2.get("font_color") | |
except: | |
pass | |
if font_color1 != None and font_color2 == None: | |
formatx.set_font_color(format1.get("font_color")) | |
elif font_color1 != None and font_color2 != None: | |
formatx.set_font_color(format1.get("font_color")) | |
elif font_color1 == None and fill_color2 != None: | |
formatx.set_font_color(format2.get("font_color")) | |
return(formatx) | |
def generate_basic_format( fileinfo , filecount , headline ): | |
global glob_dict_ROW; global glob_dict_COL; global glob_dict_CLL; global glob_dict_regex; | |
print(" -- generating basic formats --") | |
workbook = glob_workbook | |
basic_control = False | |
try: | |
basic_formats = fileinfo["basic_formats"] | |
basic_control = True | |
except: | |
print(" >> no basic formats found") | |
if basic_control == False: | |
return | |
formatcount = 0 | |
for entry in basic_formats: | |
################################################# | |
# Reading basic options | |
################################################# | |
mode = entry.get("anchor_mode") | |
format_start = 0 | |
breaking_before = 0 | |
breaking_after = 0 | |
demanded_position_list = None | |
try: | |
breaking_before = entry.get("breaking_before") | |
except: | |
pass | |
try: | |
breaking_after = entry.get("breaking_after") | |
except: | |
pass | |
try: | |
format_start = entry.get("format_range_start") | |
except: | |
pass | |
try: | |
pattern = entry.get("anchor_regex") | |
regex_x = re.compile(pattern) | |
demanded_position_list = finding_regexes(regex_x) | |
except: | |
pass | |
################################################# | |
# Casting format + loading format options into casted format | |
################################################# | |
text_format = "@" | |
try: | |
text_format = entry["text_format"] | |
except: | |
pass | |
num_format = "0.0####" | |
try: | |
num_format = entry["num_format"] | |
formaty = workbook.add_format({"num_format": num_format}) | |
except: | |
formaty = workbook.add_format() | |
formatx = workbook.add_format({"num_format": text_format}) | |
format_general = workbook.add_format() | |
### If options block. Options always present due to check_format_integrety(). | |
if entry.get("bold") == "1" or entry.get("bold") == 1: | |
formatx.set_bold() | |
formaty.set_bold() | |
format_general.set_bold() | |
if entry.get("italic") == "1" or entry.get("italic") == 1: | |
formatx.set_italic() | |
formaty.set_italic() | |
format_general.set_italic() | |
for order in entry.get("alignments"): | |
formatx.set_align(order) | |
formaty.set_align(order) | |
format_general.set_align(order) | |
if entry.get("border_top") == "1" or entry.get("border_top") == 1: | |
formatx.set_top() | |
formaty.set_top() | |
format_general.set_top() | |
if entry.get("border_bottom") == "1" or entry.get("border_bottom") == 1: | |
formatx.set_bottom() | |
formaty.set_bottom() | |
format_general.set_bottom() | |
if entry.get("border_right") == "1" or entry.get("border_right") == 1: | |
formatx.set_right() | |
formaty.set_right() | |
format_general.set_right() | |
if entry.get("border_left") == "1" or entry.get("border_left") == 1: | |
formatx.set_left() | |
formaty.set_left() | |
format_general.set_left() | |
if entry.get("text_wrap") == "1" or entry.get("text_wrap") == 1: | |
formatx.set_text_wrap() | |
formaty.set_text_wrap() | |
format_general.set_text_wrap() | |
###### | |
### Open try-except Block. For options not possible to control via check_format_integrety(). | |
try: | |
color = entry["fill_color"] | |
formatx.set_bg_color(color) | |
formaty.set_bg_color(color) | |
format_general.set_bg_color(color) | |
except: | |
pass | |
try: | |
color = entry["font_color"] | |
formatx.set_font_color(color) | |
formaty.set_font_color(color) | |
format_general.set_bg_color(color) | |
except: | |
pass | |
try: | |
breaking_before = entry["breaking_before"] | |
except: | |
pass | |
try: | |
breaking_after = entry["breaking_after"] | |
except: | |
pass | |
try: | |
format_range = entry["format_range"]+1 | |
except: | |
format_range = 1 | |
if glob_debug == True: | |
formatx.set_bg_color(glob_dict_color["text_format"][0]) | |
formaty.set_bg_color(glob_dict_color["num_format"][0]) | |
###### | |
################################################# | |
# Saving loaded formats into dicts | |
################################################# | |
### Check if regex found a matching cell (=> demanded_position_list != None). | |
# formaty.set_bg_color("pink") | |
if demanded_position_list is not None: | |
for match in demanded_position_list: | |
for e in range(0,format_range): | |
if mode == "row": | |
row = match[0]+format_start+e; col = match[1]; start = 0; end = 0; temp_dict = {}; | |
listend = len(glob_listx[col]) | |
### Checking for breaking before regex match. | |
if breaking_before == 1: | |
for col_2 in range(col,0,-1): | |
string = str(glob_listx[row][col_2-1]) | |
if string == '' or string == ' ': | |
start = col_2 | |
break | |
elif col_2 == 0: | |
start = 0 | |
else: | |
start = 0 | |
### Checking for breaking after regex match. | |
if breaking_after == 1: | |
for col_2 in range(col,listend): | |
string = str(glob_listx[row][col_2]) | |
if string == '' or string == ' ': | |
end = col_2 | |
break | |
elif col_2+1 == listend: | |
end = listend | |
else: | |
end = listend | |
### Filling the inner dictionary. | |
for c in range(start, end): | |
string = str(glob_listx[row][c]) | |
if glob_float.match(string): | |
temp_dict[c] = formaty | |
elif string.startswith("="): | |
temp_dict[c] = format_general | |
else: | |
temp_dict[c] = formatx | |
try: | |
glob_dict_ROW[row][0].update(temp_dict) | |
except: | |
### 1 is showing the script that there are several row/col informations in a following dictionary. | |
### Compare to the else case (where this value is 0). | |
glob_dict_ROW[row] = [temp_dict, formatcount] | |
elif mode == "col": | |
row = match[0]; col = match[1]+format_start+e; start = 0; end = 0; temp_dict = {}; | |
listend = len(glob_listx) | |
### Checking for breaking before regex match. | |
if breaking_before == 1: | |
for row_2 in range(row,0,-1): | |
string = str(glob_listx[row_2-1][col]) | |
if string == '' or string == ' ': | |
start = row_2 | |
break | |
elif row_2 == 0: | |
start = 0 | |
else: | |
start = 0 | |
### Checking for breaking after regex match. | |
if breaking_after == 1: | |
for row_2 in range(row,listend): | |
string = str(glob_listx[row_2][col]) | |
if string == '' or string == ' ': | |
end = row_2 | |
break | |
elif row_2+1 == listend: | |
end = listend | |
else: | |
end = listend | |
### Filling the inner dictionary. | |
for r in range(start, end): | |
string = str(glob_listx[r][col]) | |
if glob_float.match(string): | |
temp_dict[r] = formaty | |
elif string.startswith("="): | |
temp_dict[r] = format_general | |
else: | |
temp_dict[r] = formatx | |
try: | |
glob_dict_COL[col][0].update(temp_dict) | |
except: | |
### 1 is showing the script that there are several row/col informations in a following dictionary. | |
### Compare to the else case (where this value is 0). | |
glob_dict_COL[col] = [temp_dict, formatcount] | |
else: | |
row = match[0]; col = match[1] | |
demanded_tuple = (row, col,) | |
demanded_format = None | |
string = str(glob_listx[row][col]) | |
if glob_float.match(string): | |
demanded_format = formaty | |
elif string.startswith("="): | |
demanded_format = format_general | |
else: | |
demanded_format = formatx | |
glob_dict_CLL[demanded_tuple] = demanded_format | |
if glob_debug == True: | |
if mode == "row": | |
glob_dict_regex[(match[0], match[1])] = 0 | |
if mode == "col": | |
glob_dict_regex[(match[0], match[1])] = 0 | |
else: | |
### Showing the user which regex did not find a matching cell. | |
print(" >> failed to find a matching cell for \"" + str(pattern) + "\" --") | |
formatcount = formatcount + 1 | |
def finding_regexes( regex_x ): | |
listx = glob_listx | |
temp_list = [] | |
row_and_col = [] | |
control = False | |
big_listlength = len(listx) | |
for row in range(0,big_listlength): | |
listlength = len(listx[row]) | |
for col in range(0,listlength): | |
if regex_x.search(str(listx[row][col])): | |
### If a regex match is found, the control gets true. | |
control = True | |
row = row | |
col = col | |
position = [row, col] | |
temp_list.append(position) | |
if control == True: | |
return(temp_list) | |
else: | |
pass | |
def complete_correction( filepath ): | |
print(" -- generating lists -- ") | |
big_list = [] | |
temp_list = [] | |
head_length = 0 | |
linecounter = 0 | |
# Reading the informations from the file. | |
# Filling the informations into lists. | |
with open(filepath, 'r+') as basic_file: | |
for line in basic_file: | |
temp_list = line.split('\t') | |
temp_length = len(temp_list) | |
### Deleting existing \n's. | |
if temp_list[temp_length-1].endswith("\n"): | |
temp_list[temp_length-1] = temp_list[temp_length-1].rstrip() | |
###### | |
### Changing strings to floats if possible. | |
for i in temp_list: | |
if glob_float.match(i): | |
temp_list[temp_list.index(i)] = float(i) | |
###### | |
big_list.append(temp_list) | |
linecounter = linecounter + 1 | |
### Adjusting inner lists (length set to max length by adding '' entries). | |
highest_length = len(max(big_list, key=len)) | |
list_counter = 0 | |
for i in big_list: | |
if len(i) < highest_length: | |
for j in range (len(big_list[list_counter]),highest_length): | |
big_list[list_counter].append('') | |
list_counter = list_counter + 1 | |
###### | |
return(big_list) | |
def headline_finder( fileinfo , listx ): | |
headline = 0 | |
counter = 0 | |
try: | |
headline = fileinfo["headline_regex"] | |
if isinstance(headline, int): | |
pass | |
else: | |
for x in listx: | |
if headline in str(x[0]): | |
headline = counter | |
counter = counter + 1 | |
print(" -- headline found --") | |
except: | |
print(" >> no headline found --") | |
return(headline) | |
def write_sheets( fileinfo , filecount , sheet_name , filepath , zoom ): | |
global glob_listx; | |
workbook = glob_workbook | |
print(" -- generating sheet nr. " + str(filecount+1) + " -- ") | |
print(" -[ " + filepath + " realized in " + sheet_name + " ]-") | |
###### writing a sheet + generating THE list | |
sheetx = workbook.add_worksheet(sheet_name) | |
glob_listx = complete_correction(filepath) | |
listx = glob_listx | |
###### | |
headline = headline_finder(fileinfo, listx) | |
row_max = len(listx) | |
print(" -- " + str(row_max) + " entries found in: " + sheet_name + " --") | |
generate_basic_format(fileinfo, filecount, headline) | |
pattern_float = re.compile("^\-?\d+(\.\d+([e]\-\d+)?)?$") | |
try: | |
int_demanding_format = workbook.add_format({"num_format": fileinfo["int_format"]}) | |
except: | |
int_demanding_format = workbook.add_format({"num_format": "#,###"}) | |
try: | |
float_demanding_format = workbook.add_format({"num_format": fileinfo["float_format"]}) | |
except: | |
float_demanding_format = workbook.add_format({"num_format": "0.00"}) | |
text_forcing_format = workbook.add_format({"num_format": "@"}) | |
general_forcing_format = workbook.add_format() | |
regex_finding_format = workbook.add_format({"num_format": "@"}) | |
regex_conditional_format = workbook.add_format() | |
if glob_debug == True: | |
text_forcing_format.set_bg_color(glob_dict_color["text_auto"][0]) | |
int_demanding_format.set_bg_color(glob_dict_color["int_format"][0]) | |
float_demanding_format.set_bg_color(glob_dict_color["float_format"][0]) | |
regex_finding_format.set_bg_color(glob_dict_color["regex_finding"][0]) | |
regex_conditional_format.set_bg_color(glob_dict_color["regex_cond_finding"][0]) | |
generate_cond_format(fileinfo, filecount) | |
############ basic writing process (necessarily with for-for) | |
for row in range(0, row_max): | |
if glob_debug == True and row == 200: | |
break | |
for col in range(0, len(listx[row])): | |
cell_tuple = (row, col,) | |
if row in glob_dict_ROW and col in glob_dict_ROW[row][0] and col in glob_dict_COL and row in glob_dict_COL[col][0]: | |
temp_format = merge_formats(filecount, glob_dict_ROW[row][1], glob_dict_COL[col][1]) | |
sheetx.write(row, col, listx[row][col], temp_format) | |
print(" >> tried to solve a format conflict. Check row " + str(row+1) + ", col " + str(col+1)) | |
elif cell_tuple in glob_dict_CLL: | |
sheetx.write(row, col, listx[row][col], glob_dict_CLL[cell_tuple]) | |
elif row in glob_dict_ROW and col in glob_dict_ROW[row][0]: | |
sheetx.write(row, col, listx[row][col], glob_dict_ROW[row][0][col]) | |
elif col in glob_dict_COL and row in glob_dict_COL[col][0]: | |
sheetx.write(row, col, listx[row][col], glob_dict_COL[col][0][row]) | |
else: | |
if glob_float.match(str(listx[row][col])): | |
if float(listx[row][col])%1 == 0: | |
sheetx.write(row, col, listx[row][col], int_demanding_format) | |
else: | |
sheetx.write(row, col, listx[row][col], float_demanding_format) | |
elif str(listx[row][col]).startswith("="): | |
sheetx.write(row, col, listx[row][col], general_forcing_format) | |
else: | |
sheetx.write(row, col, listx[row][col], text_forcing_format) | |
if glob_debug == True: | |
if cell_tuple in glob_dict_regex: | |
if glob_dict_regex[cell_tuple] == 0: | |
sheetx.write(row, col, listx[row][col], regex_finding_format) | |
else: | |
sheetx.write(row, col, listx[row][col], regex_conditional_format) | |
############ | |
cond_format_finder_writer(sheetx, listx, fileinfo, filecount) | |
sheetx.set_zoom(zoom) | |
glob_cond_formats.clear() | |
glob_dict_ROW.clear() | |
glob_dict_COL.clear() | |
glob_dict_CLL.clear() | |
glob_dict_regex.clear() | |
try: | |
col_width = glob_dictionary["files"][filecount]["col_width"] | |
sheetx.set_column(0, col, col_width) | |
except: | |
pass | |
add_filters(sheetx, listx, fileinfo, filecount) | |
add_freeze(sheetx, listx, fileinfo, filecount) | |
def sorted_nicely( l ): | |
convert = lambda text: int(text) if text.isdigit() else text | |
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)] | |
return sorted(l, key = alphanum_key) | |
def file_take( args ): | |
global glob_filecount; | |
zoom = 85 | |
path = "." | |
entrycount = 0 | |
filecount = 0 | |
for fileinfo in glob_dictionary["files"]: | |
prefix_control = False | |
try: | |
zoom = glob_dictionary["files"][entrycount]["zoomlvl"] | |
except: | |
pass | |
regex_x = re.compile(glob_dictionary["files"][entrycount]["input_file"]) | |
path = gimme_path(entrycount) | |
sheet_prefix = "" | |
if args.flist is not None: | |
sorted_list = args.flist.split(",") | |
else: | |
file_list = [] | |
found_files = 0 | |
for file in os.listdir(path): | |
if regex_x.search(file): | |
file_list.append(file) | |
found_files = found_files + 1 | |
sorted_list = sorted_nicely(file_list) | |
try: | |
sheet_prefix = glob_dictionary["files"][entrycount]["sheet_prefix"] | |
if len(sheet_prefix) > 30: | |
sheet_prefix = sheet_prefix[0:29] | |
prefix_control = True | |
except: | |
pass | |
filecount = 0 | |
alterego_count = 2 | |
temp_list = [""] | |
for f in sorted_list: | |
if len(sorted_list) > 1: | |
if prefix_control == True: | |
sheet_name = sheet_prefix + " " + str(filecount+1) | |
write_sheets(fileinfo, entrycount, sheet_name, path+f, zoom) | |
filecount = filecount + 1 | |
else: | |
sheet_name = os.path.basename(str(f)) | |
print(sheet_name) | |
###### cutting names + checking for double names after cutting. | |
### Casting insert_control (True is standard). | |
insert_control = True | |
if len(sheet_name) > 30: | |
sheet_name = sheet_name[0:29] | |
for e in temp_list: | |
### If element from temporary file list == current sheet name: | |
### No insertation (insert_control = False); sheet name gets an adapation. | |
if e == sheet_name: | |
insert_control = False | |
### Deleting 3 more characters allows duplicates up to "-99" | |
sheet_name = sheet_name[0:26]+'-'+str(alterego_count) | |
alterego_count = alterego_count + 1 | |
if insert_control == True: | |
temp_list.append(sheet_name) | |
###### | |
write_sheets(fileinfo, entrycount, sheet_name, f, zoom) | |
filecount = filecount + 1 | |
else: | |
if prefix_control == True: | |
sheet_name = sheet_prefix | |
write_sheets(fileinfo, entrycount, sheet_name, f, zoom) | |
else: | |
sheet_name = os.path.basename(str(f)) | |
if len(sheet_name) > 30: | |
sheet_name = sheet_name[0:29] | |
write_sheets(fileinfo, entrycount, sheet_name, f, zoom) | |
filecount = filecount + 1 | |
entrycount = entrycount + 1 | |
glob_filecount = filecount | |
def check_format_integrety(): | |
list_basic_options = {"format_range_start":0, "format_range":0, "bold":0, "italic":0, "alignments":[], "border_top":0, "border_bottom":0, "border_right":0, "border_left":0, "text_wrap":0, "breaking":0} | |
list_cond_options = {"start_row":1, "start_col":0, "end_col":0, "conditional_type":"num", "conditional_numeric_midvalue":0, "conditional_numeric_midcolor":"#ffffff"} | |
files = glob_dictionary["files"] | |
basic_control = False | |
cond_control = False | |
for f in files: | |
### Checking if formats existing. | |
try: | |
basic_formats = f["basic_formats"] | |
basic_control = True | |
except: | |
pass | |
try: | |
cond_formats = f["conditional_formats"] | |
cond_control = True | |
except: | |
pass | |
###### | |
if basic_control == True: | |
for bformat in basic_formats: | |
for option in list_basic_options: | |
try: | |
test = bformat[option] | |
except: | |
bformat[option] = list_basic_options.get(option) | |
else: | |
pass | |
if cond_control == True: | |
for cformat in cond_formats: | |
for option in list_cond_options: | |
try: | |
test = cformat[option] | |
except: | |
cformat[option] = list_cond_options.get(option) | |
else: | |
pass | |
def gimme_path( entrycount ): | |
try: | |
path = glob_dictionary["files"][entrycount]["input_path"] | |
if path == "" or path == " ": | |
path = "./" | |
except: | |
path = "./" | |
return(path) | |
def situation_negotiator( output_name ): | |
complete_name = output_name + '.xlsx' | |
path = gimme_path( 0 ) | |
for file in os.listdir(path): | |
if file == complete_name: | |
print("\n >> WARNING: file \"" + complete_name + "\" in " + path + " is an regex matching \"input_file:\" doublicate. DELETE? -- ") | |
yes_no = raw_input(" ?? (y/n): ") | |
print("") | |
if yes_no == "y" or yes_no == "Y" or yes_no == "yes" or yes_no == "Yes": | |
os.remove(path+file) | |
else: | |
exit() | |
def argparsefunc(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("jfile", type=str, help="Give the JSON file name.") | |
parser.add_argument("--output", type=str, help="Type in the final excel files name.") | |
parser.add_argument("--debug", type=bool, help="Type in true or false to use the debug mode.") | |
parser.add_argument("--flist", type=str, help="Comma separated List of files") | |
try: | |
args = parser.parse_args() | |
return args | |
except: | |
parser.print_help() | |
exit() | |
def main(): | |
global glob_dictionary | |
global glob_workbook | |
global glob_float | |
global glob_debug | |
glob_float = re.compile("^\-?\d+(\.\d+([e]\-\d+)?)?$") | |
args = argparsefunc() | |
data = {} | |
with open(args.jfile, 'r+') as json_data: | |
glob_dictionary = json.load(json_data) | |
if args.debug == True: | |
glob_debug = True | |
print("\n--------- DEBUG MODE -----------") | |
else: | |
glob_debug == False | |
output_name = args.output | |
if args.output is None: | |
output_name = str(glob_dictionary["files"][0]["input_file"]) | |
situation_negotiator(output_name) | |
starttime = datetime.datetime.now() | |
print starttime.strftime("\n -t %H:%M:%S (h/m/s)\n") | |
check_format_integrety() | |
### basic commands to create the workbook | |
workbook = xlsxwriter.Workbook(output_name + '.xlsx', {'constant_memory': True}) | |
glob_workbook = workbook | |
file_take(args) | |
workbook.close() | |
###### | |
endtime = datetime.datetime.now() | |
timedif = endtime - starttime | |
print endtime.strftime("\n -t %H:%M:%S (h/m/s)\n") | |
print(" FINISHED") | |
print(" o duration: " + str(timedif) + ' (h/m/s)') | |
print(" o created sheets: " + str(glob_filecount+1)) | |
print(" o Saved as: " + output_name + '.xlsx') | |
if glob_debug == True: | |
print("\n--------- DEBUG MODE -----------\n") | |
for e in glob_dict_color: | |
print(str(glob_dict_color[e][0]) + " (" + str(glob_dict_color[e][1]) + "): " + str(glob_dict_color[e][2])) | |
print("\n") | |
if __name__ == "__main__": | |
from decimal import * | |
import xlsxwriter | |
import datetime | |
import argparse | |
import numbers | |
import json | |
import re | |
import os | |
main() |