配置文件的自动化更新(v5.1)

import openpyxl
import os
import shutil
# import pandas as pd
import datetime


wb_temp = openpyxl.load_workbook("E:Projectswork_11_25\Utility機種対応知能化\Utility機種対応仕様.xlsx")
wb_info = openpyxl.load_workbook("E:Projectswork_11_25\Utility機種対応知能化\Utility機種対応Input情報.xlsx")
wb_result = openpyxl.load_workbook("E:Projectswork_11_25\Utility機種対応知能化\Utility詳細設計書_テンプレート_結果記入.xlsx")

ws_base = wb_temp["Color Correct Utility"]
ws_code = wb_temp["LANGCode"]
ws_info = wb_info["Color Correct Utility"]

# 复制原文件夹到新路径

value_MainVersion = ws_info['C9'].value
value_SubVersion = ws_info['C10'].value
folder_name = ""
value_MainVersion = value_MainVersion.split('.')
for v in value_MainVersion:
    if len(v) == 1:
        v = v.zfill(2)
    folder_name += v
folder_name = folder_name + "_" + str(value_SubVersion)
# print(folder_name)
# 022500_0

source_path = os.path.abspath(r'E:Projectswork_11_25Utility機種対応知能化CCUBaseCCU_ALL_022401_1')
target_path = os.path.abspath(r'E:Projectswork_11_25Utility機種対応知能化CCU_ALL_' + folder_name)

if not os.path.exists(target_path):
    # 如果目标路径不存在原文件夹的话就创建
    os.makedirs(target_path)

if os.path.exists(source_path):
    # 如果目标路径存在原文件夹的话就先删除
    shutil.rmtree(target_path)

shutil.copytree(source_path, target_path)
print('copy dir finished!')


# 多国语情报:
language_code = [ws_base['B22'].value, ws_base['B25'].value, ws_base['B28'].value, ws_base['B31'].value,
                 ws_base['B34'].value, ws_base['B37'].value, ws_base['B40'].value, ws_base['B43'].value,
                 ws_base['B46'].value, ws_base['B49'].value, ]

# ['ENU', 'JPN', 'CHS', 'CHT', 'DEU', 'ESP', 'FRA', 'ITA', 'KOR', 'PTB']

# 多国语编码
language_encoding = {}
for l in range(3, 26):
    language_encoding.update({ws_code['B' + str(l)].value: ws_code['C' + str(l)].value})
# print(language_encoding)

# OS情报:
OS_info = [ws_base['B56'].value, ws_base['B57'].value]
# ['x86', 'x64']

path = target_path + '\' + ws_base['C4'].value
path_readme = target_path + '\' + ws_base['C12'].value
# setupdirOS情報Resource多国語情報ColorUtilityOclutl.ini

path_list_dic = {}
path_list = []
path_list_readme = {}
readme_map = {'ENU': 'readme', 'JPN': 'readme', 'CHS': 'readme', 'CHT': 'readme', 'DEU': 'Liesmich', 'ESP': 'leame',
              'FRA': 'LisezMoi', 'ITA': 'Leggimi', 'KOR': 'readme', 'PTB': 'readme', }

for lan in language_code:
    for os in OS_info:
        path_list_dic[lan + '_' + os] = path.replace('多国語情報', lan).replace('OS情報', os)
    path_list_readme[lan] = path_readme.replace('多国語情報', lan).replace('ReadmeFile', readme_map[lan])
# print(path_list_readme)
# {'ENU': 'setupdir\license\ENU\readme.txt',
#  'JPN': 'setupdir\license\JPN\readme.txt',
#  'CHS': 'setupdir\license\CHS\readme.txt',
#  'CHT': 'setupdir\license\CHT\readme.txt',
#  'DEU': 'setupdir\license\DEU\Liesmich.txt',
#  'ESP': 'setupdir\license\ESP\license.txt',
#  'FRA': 'setupdir\license\FRA\LisezMoi.txt',
#  'ITA': 'setupdir\license\ITA\license.txt',
#  'KOR': 'setupdir\license\KOR\readme.txt',
#  'PTB': 'setupdir\license\PTB\readme.txt'}

# print(path_list_dic)
# {'ENU_x86': 'setupdir\x86\Resource\ENU\ColorUtility\Oclutl.ini',
# 'JPN_x86': 'setupdir\x86\Resource\JPN\ColorUtility\Oclutl.ini',
# 'CHS_x86': 'setupdir\x86\Resource\CHS\ColorUtility\Oclutl.ini',
# 'CHT_x86': 'setupdir\x86\Resource\CHT\ColorUtility\Oclutl.ini',
# 'DEU_x86': 'setupdir\x86\Resource\DEU\ColorUtility\Oclutl.ini',
# 'ESP_x86': 'setupdir\x86\Resource\ESP\ColorUtility\Oclutl.ini',
# 'FRA_x86': 'setupdir\x86\Resource\FRA\ColorUtility\Oclutl.ini',
# 'ITA_x86': 'setupdir\x86\Resource\ITA\ColorUtility\Oclutl.ini',
# 'KOR_x86': 'setupdir\x86\Resource\KOR\ColorUtility\Oclutl.ini',
# 'PTB_x86': 'setupdir\x86\Resource\PTB\ColorUtility\Oclutl.ini',
# 'ENU_x64': 'setupdir\x64\Resource\ENU\ColorUtility\Oclutl.ini',
# 'JPN_x64': 'setupdir\x64\Resource\JPN\ColorUtility\Oclutl.ini',
# 'CHS_x64': 'setupdir\x64\Resource\CHS\ColorUtility\Oclutl.ini',
# 'CHT_x64': 'setupdir\x64\Resource\CHT\ColorUtility\Oclutl.ini',
# 'DEU_x64': 'setupdir\x64\Resource\DEU\ColorUtility\Oclutl.ini',
# 'ESP_x64': 'setupdir\x64\Resource\ESP\ColorUtility\Oclutl.ini',
# 'FRA_x64': 'setupdir\x64\Resource\FRA\ColorUtility\Oclutl.ini',
# 'ITA_x64': 'setupdir\x64\Resource\ITA\ColorUtility\Oclutl.ini',
# 'KOR_x64': 'setupdir\x64\Resource\KOR\ColorUtility\Oclutl.ini',
# 'PTB_x64': 'setupdir\x64\Resource\PTB\ColorUtility\Oclutl.ini'}
# if p[3:] == "_x64":
dic_x86 = {}
dic_x64 = {}
for k in language_code:
    dic_x86[k + '_x86'] = path_list_dic[k + '_x86']
    dic_x64[k + '_x64'] = path_list_dic[k + '_x64']
# print(dic_x64)
# print(dic_x86)


def add_x86():
    m = 0
    for p in dic_x86:
        pd = open("log.txt", "a+", encoding='utf-8')
        update_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        with open(dic_x86[p], 'r', encoding=language_encoding[p[:-4]]) as f1:
            new_section_name = '[CCMVer' + str(ws_info['C3'].value) + ']'  # 获取H列单元格内容"section"
            # [CCMVer42.3]
            lst = f1.readlines()  # 读取每行信息
            if new_section_name+'
' in lst:
                comment = "该section在x86文件中已添加过,无需添加。"
                print(comment + ' ---------------- ' + update_time)
                pd.write(dic_x86[p] + '
' + comment + ' ---------------- ' + update_time + '
')
                # sys.exit(0)
                break
            # #
            n = lst.index('[StringSet1]
')  # 获取"[StringSet1]"的索引
            lst.insert(n, new_section_name + '
')  # 将新section按照索引值插入列表中
            pd.write(dic_x86[p] + '
')
            pd.write(new_section_name + '
')
            if ws_info['C8'].value == 'Default':
                for i in range(5, 12):
                    n = n + 1
                    item = ws_base['E' + str(i)].value               # 获取item值
                    value = str(ws_info['C' + str(i-1)].value)         # 获取value值
                    if i == 8:
                        value = value + "
"
                    if i == 9:
                        value = str(ws_base['D' + str(22 + m)].value)
                    if i == 10:
                        value = str(ws_base['D' + str(23 + m)].value)
                    if i == 11:
                        value = str(ws_base['D' + str(24 + m)].value) + "
"
                        m += 3
                    lst.insert(n, item + "=" + value + '
')  # 将item值与value值按照索引插入列表中
                    pd.write(item + "=" + value + '
')
                pd.write('update time ---------------- ' + update_time + '


')
            elif ws_info['C8'].value == 'A6':
                for i in range(5, 12):
                    n = n + 1
                    item = ws_base['E' + str(i)].value  # 获取item值
                    value = str(ws_info['C' + str(i - 1)].value)  # 获取value值
                    if i == 8:
                        value = value + "
"
                    if i == 9:
                        value = str(ws_base['E' + str(22 + m)].value)
                    if i == 10:
                        value = str(ws_base['E' + str(23 + m)].value)
                    if i == 11:
                        value = str(ws_base['E' + str(24 + m)].value) + "
"
                        m += 3
                    lst.insert(n, item + "=" + value + '
')         # 将item值与value值按照索引插入列表中
                    pd.write(item + "=" + value + '
')
                pd.write('update time ---------------- ' + update_time + '


')
        with open(dic_x86[p], 'w', encoding=language_encoding[p[:-4]]) as f2:
            f2.writelines(lst)
        pd.close()


def add_x64():
    m = 0
    for p in dic_x64:
        pd = open("log.txt", "a+", encoding='utf-8')
        update_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        with open(dic_x64[p], 'r', encoding=language_encoding[p[:-4]]) as f1:
            new_section_name = '[CCMVer' + str(ws_info['C3'].value) + ']'  # 获取H列单元格内容"section"
            # [CCMVer42.3]
            lst = f1.readlines()  # 读取每行信息
            if new_section_name+'
' in lst:
                comment = "该section在x64文件中已添加过,无需添加。"
                print(comment + ' ---------------- ' + update_time)
                pd.write(dic_x64[p] + '
' + comment + ' ---------------- ' + update_time + '
')
                break
            # #
            n = lst.index('[StringSet1]
')  # 获取"[StringSet1]"的索引
            lst.insert(n, new_section_name + '
')  # 将新section按照索引值插入列表中
            pd.write(dic_x64[p] + '
')
            pd.write(new_section_name + '
')
            if ws_info['C8'].value == 'Default':
                for i in range(5, 12):
                    n = n + 1
                    item = ws_base['E' + str(i)].value  # 获取item值
                    value = str(ws_info['C' + str(i - 1)].value)  # 获取value值
                    if i == 8:
                        value = value + "
"
                    if i == 9:
                        value = str(ws_base['D' + str(22 + m)].value)
                    if i == 10:
                        value = str(ws_base['D' + str(23 + m)].value)
                    if i == 11:
                        value = str(ws_base['D' + str(24 + m)].value) + "
"
                        m += 3
                    lst.insert(n, item + "=" + value + '
')  # 将item值与value值按照索引插入列表中
                    pd.write(item + "=" + value + '
')
                pd.write('update time ---------------- ' + update_time + '


')
            elif ws_info['C8'].value == 'A6':
                for i in range(5, 12):
                    n = n + 1
                    item = ws_base['E' + str(i)].value  # 获取item值
                    value = str(ws_info['C' + str(i - 1)].value)  # 获取value值
                    if i == 8:
                        value = value + "
"
                    if i == 9:
                        value = str(ws_base['E' + str(22 + m)].value)
                    if i == 10:
                        value = str(ws_base['E' + str(23 + m)].value)
                    if i == 11:
                        value = str(ws_base['E' + str(24 + m)].value) + "
"
                        m += 3
                    lst.insert(n, item + "=" + value + '
')  # 将item值与value值按照索引插入列表中
                    pd.write(item + "=" + value + '
')
                pd.write('update time ---------------- ' + update_time + '


')
        with open(dic_x64[p], 'w', encoding=language_encoding[p[:-4]]) as f2:
            f2.writelines(lst)
        pd.close()


change_type_value = ws_base['B4'].value
if change_type_value == 'CCMVerion追加':
    add_x86()
    add_x64()


def readme():
    for r in path_list_readme:
        pd = open("log.txt", "a+", encoding='utf-8')
        update_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        with open(path_list_readme[r], 'r+', encoding=language_encoding[r]) as f3:
            line = f3.readlines()
            index_list = []
            # [' Windows 10 operating system
', ' Windows 8.1 operating system
', ' Windows 8 operating system
',
            #  ' Windows 7 operating system
', ' Windows Server 2019 operating system
',
            #  ' Windows Server 2016 operating system
', ' Windows Server 2012 R2 operating system
',
            #  ' Windows Server 2012 operating system
', ' Windows Server 2008 R2 operating system
',
            #  ' Windows Server 2008 operating system
']
            for i in line:
                if 'Version' in i:
                    ver = line.index(i)
                    line[ver] = "                         Version " + ws_info['C9'].value + '
'
                    pd.write(path_list_readme[r] + '
' + line[ver])
                if 'Copyright' in i:
                    cr = line.index(i)
                    line[cr] = "              " + ws_info['C12'].value + '
'
                    pd.write(line[cr])
                if 'Windows' in i and 'operating system' in i:
                    index_list.append(line.index(i))
            line = [line[c] for c in range(len(line)) if (c not in index_list)]
            line.insert(index_list[0], ws_info['C11'].value+'
')
            pd.write(ws_info['C11'].value + 'update time ---------------- ' + update_time + '

')
        pd.close()

        with open(path_list_readme[r], 'w', encoding=language_encoding[r]) as f4:
            f4.writelines(line)
    print('readme done')


readme()


def version_modify():
    pd = open("log.txt", "a+", encoding='utf-8')
    with open(target_path + '\' + 'version.txt', 'r', encoding='utf-8') as f5:
        line = f5.readlines()
        line[2] = 'MainVersion=' + ws_info['C9'].value + '
'
        line[3] = 'SubVersion=' + str(ws_info['C10'].value) + '
'
        line[4] = 'Copyright=' + ws_info['C12'].value
        update_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        pd.write(target_path + '\' + 'version.txt' + '
' + line[2])
        pd.write(line[3])
        pd.write(line[4] + 'update time ---------------- ' + update_time + '

')
    pd.close()

    with open(target_path + '\' + 'version.txt', 'w', encoding='utf-8') as f6:
        f6.writelines(line)


version_modify()
原文地址:https://www.cnblogs.com/wangzhilong/p/11990356.html