#!/usr/bin/python3 import sys from openpyxl import load_workbook import json # Load JSON file content into data dictionary def load_input_format(): with open('alarms_format.json') as json_file: dic = json.load(json_file) return dic dic_data_format = load_input_format() # Default source and destination values for a local run #src = "Alarms.xlsx" #dst = "/home/denali/Projects/application/resources/settings/Alarms/Alarms.conf" try: # Source is the first input argument # Destination is the second input argument # NOTE: try and except was used to make sure when this script is run locally, it can be called without passing any # input arguments even as empty strings. In case of an exception do nothing and just pass. src = sys.argv[1] dst = sys.argv[2] except: print("No argv[] parameters defined, using values from JSON file") src = dic_data_format['source_data'] dst = dic_data_format['destination'] pass exl = load_workbook(src) if 'Alarms' in exl.sheetnames: alarms = exl['Alarms' ] else: print('Alarms sheet not found') col_alarms_id = dic_data_format['col_alarms_id'] #9 col_alarms_title = dic_data_format['col_alarms_title'] #8 col_alarms_message = dic_data_format['col_alarms_message'] #10 # it has been assumed that for each alarm multiple lines of instruction will follow. # there are a lot of other options to get instruction but the output CSV format/structure is important. # this format is chosen because works with current excel sheet with no instruction defined. # as example : order is not following the actual/current sheet indexing, just take it as an example. # # # col_instruction_title = dic_data_format['col_instruction_title'] #5 col_instruction_image = dic_data_format['col_instruction_image'] #6 conf_content = "" row = dic_data_format['row_starting_data'] #2 first row is title header val_alarm_id_prv = None val_alarm_id_cur = alarms.cell(row=row, column=col_alarms_id ).value val_alarm_id_hdr = True while val_alarm_id_cur != None: if val_alarm_id_hdr: conf_content += "[{}]\n" .format(val_alarm_id_cur ) val = alarms.cell(row=row, column=col_alarms_title ).value val_alarm_title = "" if val == None or val == 'N/A' else val.strip() val = alarms.cell(row=row, column=col_alarms_message ).value val_alarm_message = "" if val == None or val == 'N/A' else val.strip() conf_content += "Title = {}\n" .format(val_alarm_title ) conf_content += "Message = {}\n" .format(val_alarm_message ) val = alarms.cell(row=row, column=col_instruction_title ).value val_instruction_title = "" if val == None or val == 'N/A' else val.strip() val = alarms.cell(row=row, column=col_instruction_image ).value val_instruction_image = "" if val == None or val == 'N/A' else val.strip() if val_instruction_title != "" or val_instruction_image != "": conf_content += "{} = {}\n" .format(val_instruction_title, val_instruction_image ) row += 1 val_alarm_id_cur = alarms.cell(row=row, column=col_alarms_id ).value val_alarm_id_hdr = val_alarm_id_cur != val_alarm_id_prv val_alarm_id_prv = val_alarm_id_cur if val_alarm_id_hdr: conf_content += '\n' cnf = open(dst, "w") cnf.write(conf_content) cnf.close()