import sys import re from openpyxl import load_workbook from pprint import pprint from collections import defaultdict def merge_common(lists): neigh = defaultdict(set) visited = set() for each in lists: for item in each: neigh[item].update(each) def comp(node, neigh = neigh, visited = visited, vis = visited.add): nodes = set([node]) next_node = nodes.pop while nodes: node = next_node() vis(node) nodes |= neigh[node] - visited yield node for node in neigh: if node not in visited: yield sorted(comp(node)) def parse_results(results): output = {'gl': '-', 'ir': '-', 'ti': '-', 'up': '-'} for r in results: parts = r.split(' - ') if parts[1] == 'GLOOMY': output['gl'] = parts[0] elif parts[1] == 'IRRITABLE': output['ir'] = parts[0] elif parts[1] == 'TIMID': output['ti'] = parts[0] else: output['up'] = parts[0] return output def generate_result(result, color): if result == "GOOD": output = "GOOD
🎶
" elif result == "OK": output = "OK
💦
" elif result == "BAD": output = "BAD
💢
" elif result == "-": output = "-
" else: output = "{}
".format(result) class_name = 'result' if color: class_name = 'result unconfirmed' return "\n\t\t
{}".format(class_name, output) def create_table(q, tags, top_a, mid_a, bot_a, top_r, mid_r, bot_r, color_top_r, color_mid_r, color_bot_r): question = '\n\t\n\t\t{}\n\t'.format(q) header = "\n\t\n\t\t" + "\n\t\tgloomy" + "\n\t\tirritable" + "\n\t\ttimid" + "\n\t\tupbeat\n\t" top = "\n\t\n\t\t" + top_a + "" + generate_result(top_r['gl'], color_top_r['gl']) + generate_result(top_r['ir'], color_top_r['ir']) + generate_result(top_r['ti'], color_top_r['ti']) + generate_result(top_r['up'], color_top_r['up']) + "\n\t" mid = "\n\t\n\t\t" + mid_a + "" + generate_result(mid_r['gl'], color_mid_r['gl']) + generate_result(mid_r['ir'], color_mid_r['ir']) + generate_result(mid_r['ti'], color_mid_r['ti']) + generate_result(mid_r['up'], color_mid_r['up']) + "\n\t" bot = "\n\t\n\t\t" + bot_a + "" + generate_result(bot_r['gl'], color_bot_r['gl']) + generate_result(bot_r['ir'], color_bot_r['ir']) + generate_result(bot_r['ti'], color_bot_r['ti']) + generate_result(bot_r['up'], color_bot_r['up']) + "\n\t" if tags == "": return '\n{}{}{}{}{}\n
'.format(question, header, top, mid, bot) else: return '\n{}{}{}{}{}\n
'.format(tags, question, header, top, mid, bot) def filter_list(unique_shadows): output = '\t\t\t\t\n\t\t\t\t' for shadow in sorted(unique_shadows): tag = shadow.replace(' ', '_') btn = '\n\t\t\t\t'.format(tag, shadow) output += btn return output def convert_csv_to_new_tables(datafilename, outputfilename, indexfilename, royalFlag): if royalFlag: print("Generating ROYAL Tables...") else: print("Generating ORIGINAL Tables...") wb = load_workbook(datafilename) ws = wb.active outputfile = open(outputfilename, 'w', encoding="utf8") indexfile = open(indexfilename, encoding="utf8") table_text = [] unique_shadows = [] all_shadow_groups = [] for r, row in enumerate(ws.values): shadows = row[1] question_text = row[2] first_text = row[4] first_res = { 'gl': row[5], 'ir': row[6], 'ti': row[7], 'up': row[8] } color_first_res = { 'gl': False if ws.cell(row=r+1, column=6).fill.start_color.index == 'FFFFFF00' or row[5] == '-' else True, 'ir': False if ws.cell(row=r+1, column=7).fill.start_color.index == 'FFFFFF00' or row[6] == '-' else True, 'ti': False if ws.cell(row=r+1, column=8).fill.start_color.index == 'FFFFFF00' or row[7] == '-' else True, 'up': False if ws.cell(row=r+1, column=9).fill.start_color.index == 'FFFFFF00' or row[8] == '-' else True } second_text = row[9] second_res = { 'gl': row[10], 'ir': row[11], 'ti': row[12], 'up': row[13] } color_second_res = { 'gl': False if ws.cell(row=r+1, column=11).fill.start_color.index == 'FFFFFF00' or row[10] == '-' else True, 'ir': False if ws.cell(row=r+1, column=12).fill.start_color.index == 'FFFFFF00' or row[11] == '-' else True, 'ti': False if ws.cell(row=r+1, column=13).fill.start_color.index == 'FFFFFF00' or row[12] == '-' else True, 'up': False if ws.cell(row=r+1, column=14).fill.start_color.index == 'FFFFFF00' or row[13] == '-' else True } third_text = row[14] third_res = { 'gl': row[15], 'ir': row[16], 'ti': row[17], 'up': row[18] } color_third_res = { 'gl': False if ws.cell(row=r+1, column=16).fill.start_color.index == 'FFFFFF00' or row[15] == '-' else True, 'ir': False if ws.cell(row=r+1, column=17).fill.start_color.index == 'FFFFFF00' or row[16] == '-' else True, 'ti': False if ws.cell(row=r+1, column=18).fill.start_color.index == 'FFFFFF00' or row[17] == '-' else True, 'up': False if ws.cell(row=r+1, column=19).fill.start_color.index == 'FFFFFF00' or row[18] == '-' else True } if shadows != None and r != 0: tags = shadows.split(', ') for t, tag in enumerate(tags): if tag not in unique_shadows and tag != " ": unique_shadows.append(tag) tags[t] = tag.replace(' ', '_') tags = 'filterDiv ' + ' '.join(tags) else: tags = 'filterDiv None' if shadows != None and r != 0: new_tags = shadows.split(', ') for t, new_tag in enumerate(new_tags): new_tags[t] = new_tag.replace(' ', '_') all_shadow_groups.append(new_tags) if r != 0: table_text.append(create_table(question_text, tags, first_text, second_text, third_text, first_res, second_res, third_res, color_first_res, color_second_res, color_third_res)) indexHTML = indexfile.read().split('
') top_text = indexHTML[0] top_text = re.sub(r'
(\s*\n' + filter_list(unique_shadows), top_text) outputfile.write(top_text) outputfile.write('
') for table in table_text: outputfile.write(table) outputfile.write("\n
") indexHTML_script = indexHTML[1].split('') bot_text = indexHTML_script[1] outputfile.write('\n\n') outputfile.write(bot_text) outputfile.close() Output = list(merge_common(all_shadow_groups)) if royalFlag: print("---- Finished ROYAL Tables ----") # pprint(Output) else: print("---- Finished ORIGINAL Tables ----") # pprint(Output) royal_file = 'data/persona-5-royal-questions.xlsx' royal_output = 'data/output-royal.html' royal_index = 'royal.md' original_file = 'data/persona-5-questions.xlsx' original_output = 'data/output-original.html' original_index = 'index.md' convert_csv_to_new_tables(royal_file, royal_output, royal_index, True) convert_csv_to_new_tables(original_file, original_output, original_index, False)