其他分享
首页 > 其他分享> > 配置文件的自动化更新(v1.0)

配置文件的自动化更新(v1.0)

作者:互联网

 1 # 导入openpyxl和configparser模块
 2 import openpyxl
 3 from configparser import ConfigParser
 4 import os
 5 
 6 wb = openpyxl.load_workbook("SW知能化-Utility.xlsx")                             # 打开excel文件,获取工作簿对象
 7 
 8 ws = wb["Utility機種対応"]                                                       # 获取指定的表单(sheet)对象
 9 
10 language_code = ['CHS', 'CHT', 'DEU', 'ENU', 'ESP', 'FRA', 'ITA', 'JPN', 'KOR', 'PTB']
11 language_coding = {'CAT': 'windows-1252', 'CHS': 'ISO-8859-1', 'CHT': 'ISO-8859-1', 'CSY': 'Windows-1250', 'DAN': 'Windows-1252',
12                    'DEU': 'Windows-1252', 'ELL': 'Windows-1253', 'ENU': 'Windows-1252', 'ESP': 'Windows-1252',
13                    'FIN': 'Windows-1252', 'FRA': 'Windows-1252', 'HUN': 'Windows-1250', 'ITA': 'Windows-1252',
14                    'JPN': 'shift_jis', 'KOR': 'ks_c_5601-1987', 'NLD': 'Windows-1252', 'NOR': 'Windows-1252',
15                    'PLK': 'Windows-1250', 'PTB': 'Windows-1252', 'PTG': 'Windows-1252', 'RUS': 'Windows-1251',
16                    'SVE': 'Windows-1252', 'TRK': 'Windows-1254'}
17 
18 path = ws['F4'].value                                                            # 获取F列单元格内容(目标配置文件路径)
19 path_list_dic = {}
20 path_list = []
21 
22 if '...' in path:
23     path = path.replace('...\\', 'E:\Projects\\work_1016-1031\CCU_ALL\\')
24 
25 for lan in language_code:
26     path_list_dic[lan] = path.replace('ENU', lan)
27     path_list.append(path.replace('ENU', lan))
28 
29     if lan == 'JPN':
30         J8 = ws['J8'].value.replace(ws['J8'].value[-5], 'j')
31         J9 = ws['J9'].value.replace(ws['J9'].value[-5], 'j')
32         J10 = ws['J10'].value.replace(ws['J10'].value[-5], 'j')
33     elif lan == 'PTB':
34         J8 = ws['J8'].value.replace(ws['J8'].value[-5], 'por')
35         J9 = ws['J9'].value.replace(ws['J9'].value[-5], 'por')
36         J10 = ws['J10'].value.replace(ws['J10'].value[-5], 'por')
37     else:
38         J8 = ws['J8'].value.replace(ws['J8'].value[-5], lan.lower())
39         J9 = ws['J9'].value.replace(ws['J9'].value[-5], lan.lower())
40         J10 = ws['J10'].value.replace(ws['J10'].value[-5], lan.lower())
41 # print(path_list_dic)
42 # print(path_list)
43 
44 
45 def add():
46     for new_path in path_list_dic:
47         # print(new_path)
48         # print(path_list_dic[new_path])
49         # print(language_coding[new_path])
50         with open(path_list_dic[new_path], 'r', encoding=language_coding[new_path]) as f1:
51             new_section = ws['H4'].value                    # 获取H列单元格内容"section"
52             lst = f1.readlines()                            # 读取每行信息
53 #
54             n = lst.index('[StringSet1]\n')                 # 获取"[StringSet1]"的索引
55             lst.insert(n, new_section + '\n')                      # 将新section按照索引值插入列表中
56             for i in range(4, 11):
57                 n = n + 1
58                 item = ws['I' + str(i)].value               # 获取item值
59                 value = str(ws['J' + str(i)].value)         # 获取value值
60                 # if i == 7:
61                 #     value = value + "\n"
62                 if i == 8:
63                     value = J8
64                 elif i == 9:
65                     value = J9
66                 elif i == 10:
67                     value = J10 + "\n"
68                 lst.insert(n, item + " = " + value + '\n')         # 将item值与value值按照索引插入列表中
69             # print(lst)
70         with open(path_list_dic[new_path], 'w', encoding=language_coding[new_path]) as f2:
71             f2.writelines(lst)
72 
73 
74 # 获取G列单元格内容,并根据不同值,进行不同函数操作
75 change_type_value = ws['G4'].value
76 
77 if change_type_value == 'Add':
78     # cfg.write(open(path, 'a'))
79     add()
80 # elif change_type_value == 'Edit':
81 #     pass
82 # elif change_type_value == 'Delete':
83 #     pass
84 # else:
85 #     pass

 

标签:配置文件,Windows,value,replace,v1.0,1252,ws,自动化,path
来源: https://www.cnblogs.com/wangzhilong/p/11697721.html