#!/usr/bin/python3 from openpyxl import load_workbook src = "Alarms.xlsx" dst = "/home/denali/Projects/application/resources/settings/Alarms/Alarms.conf" exl = load_workbook(src) if 'Alarms' in exl.sheetnames: alarms = exl['Alarms' ] else: print('Alarms sheet not found') col_alarms_id = 9 col_alarms_title = 6 col_alarms_message = 7 # 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 = 5 # just as an example col_instruction_image = 10 # just as an example conf_content = "" row = 2 # firs row is title 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()