import json import openpyxl coallitions= ['allied', 'axis'] # These need to be manually populated in case any plane has been renamed or added planesWithMods = [''] # These list contains the root strings for any planes which also have modifications. Extra string after the root will be put into the table cell ignoredPlanes = [''] def processRPS(rpsRaw, coallition): # Function that takes the raw json data and converts it to a planesets dictionary, with plane name as key and a list of planeset indices as a value planesets = dict() index = [] # Loop through and put raw json rps into dictionaries for i in range(len(rpsRaw)): for plane in list(rpsRaw[i][coallition].keys()): planesets.setdefault(plane, []).append(rpsRaw[i]["index"]+1) # Sort the rps indices from smallest to largezt for plane in planesets: planesets[plane].sort() # Sort the planesets so that earliest appearing planes come first in the table planesets=dict(sorted(planesets.items(), key = lambda index: (index[1], index[-1]))) return planesets def fillTable(rpsRaw, ws, planesets, coallition, starting_row, moddedPlanes, ignoredPlanes): # Function that generates the excel table. Takes planesets as inout, generates excel table and returns tne last row of the table row = 2 + starting_row # Create column header for columnheader in range(1,len(rpsRaw)+1): ws.cell(row=1, column=columnheader+1, value="Tankset " + str(columnheader)) # Styles for cell if coallition == 'allied': cellFill = openpyxl.styles.PatternFill(start_color='FFFF0000', end_color='FFFF0000', fill_type='solid') else: cellFill = openpyxl.styles.PatternFill(start_color='FF0000FF', end_color='FF0000FF', fill_type='solid') whiteFont = openpyxl.styles.Font(color='FFFFFFFF') thin=openpyxl.styles.Side(border_style="thin", color="FF000000") borderGrid = openpyxl.styles.Border(top=thin, bottom=thin, left=thin, right=thin) for plane in planesets: for index in planesets[plane]: ws.cell(row=row, column=1, value=plane) ws.cell(row=row, column=len(rpsRaw)+2, value=plane) ws.cell(row=row, column=index+1).fill = cellFill row = row +1 # Generate gridlines for the table for tablerow in range(1,row): for tablecolumn in range(1,len(rpsRaw)+3): ws.cell(row=tablerow, column=tablecolumn).border = borderGrid return row theatres = ['Eastern', 'Western'] # Create the excel workbook wb = openpyxl.Workbook() # Write tables to each different sheet for Eastern and Western fromts for theatre in theatres: # Load json file with open("rts" + theatre + "Front.json", 'r') as f: rpsRaw = json.load(f) # Create worksheet ws = wb.create_sheet(theatre) i=0 for coallition in coallitions: planesets = dict() planesets = processRPS(rpsRaw, coallition) print(planesets) if i==0: # Add rps table for allies lastRow = fillTable(rpsRaw, ws, planesets, coallition, 0, planesWithMods, ignoredPlanes) else: # Add rps table for axis right after the allied table fillTable(rpsRaw, ws, planesets, coallition, lastRow-2, planesWithMods, ignoredPlanes) i = i+1 # Generate the excel file wb.save('FVP_RTS.xlsx')