配置文件的自动化更新(v4.1)
1 import openpyxl 2 import configparser 3 4 5 conf = configparser.ConfigParser() 6 7 conf.read("E:\Projects\work_11_25\\Utility機種対応知能化\\Network Extension\\NETX_ALL_010500_6\setupdir\lang\JPN\opnedef.str", encoding='shift_jis') 8 sections = conf.sections() 9 10 # ['common', 'StrTable0', 'StrTable1', 'StrTable2', 'StrTable3', 'StrTable4', 'StrTable5', 'StrTable6', 'StrTable7', 'StrTable8', 'StrTable9'] 11 12 wb = openpyxl.load_workbook("E:\Projects\work_11_25\\Utility機種対応知能化\\Utility機種対応仕様.xlsx") 13 14 ws = wb["NetworkExtension"] 15 16 17 18 with open("E:\Projects\work_11_25\\Utility機種対応知能化\\Network Extension\\NETX_ALL_010500_6\setupdir\lang\JPN\opnedef.str", "r", encoding="shift_jis") as f1: 19 lines = f1.readlines() 20 index_list = [] 21 for line in lines[4:]: 22 if '[' in line and ']' in line: 23 index_list.append(lines.index(line)) 24 25 index_list.append(len(lines) + 1) 26 kv_list = [] 27 options = [] 28 value = [] 29 for i in range(len(index_list) - 1): 30 items = lines[index_list[i]: index_list[i + 1] - 1] 31 for kv in items[1:]: 32 kv = kv.split('=') 33 options.append(kv[0]) 34 if kv == ['\n']: 35 kv.append('\n') 36 value.append(kv[1].strip()) 37 # print(options) 38 # print(value) 39 40 ws['E4'].value = lines[0]+lines[1]+lines[2] 41 # print("head完成") 42 43 44 n = 5 45 for a in options: 46 ws["D" + str(n)].value = a 47 n += 1 48 49 n = 5 50 for b in value: 51 ws["E" + str(n)].value = b 52 n += 1 53 54 m = 0 55 x = -1 56 for s in sections: 57 ws['C' + str(index_list[m] - x)].value = s 58 x += 2 59 m += 1 60 61 wb.save("test.xlsx")