manage_generate_excel.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import json
  2. import pandas as pd
  3. from openpyxl import load_workbook
  4. from openpyxl.styles import PatternFill
  5. import os
  6. output_folder="output_manage"
  7. def read_from_json(file_path):
  8. # 从JSON文件中读取内容
  9. with open(file_path, 'r', encoding='utf-8') as file:
  10. data = json.load(file)
  11. return data
  12. # 递归函数,遍历所有终节点并将全路径和值存入字典
  13. def get_leaf_paths(data, path="", result=None):
  14. # 不需要翻译的Key
  15. ignore_data = ['app.name', 'mine.setting.chinese',
  16. 'mine.setting.english', 'mine.setting.enth']
  17. if result is None:
  18. result = {}
  19. if isinstance(data, dict):
  20. # 如果是字典,继续遍历字典中的每个键
  21. for key, value in data.items():
  22. current_path = f"{path}.{key}" if path else key
  23. get_leaf_paths(value, current_path, result)
  24. elif isinstance(data, list):
  25. # 如果是列表,遍历每个元素
  26. for index, item in enumerate(data):
  27. current_path = f"{path}[{index}]"
  28. get_leaf_paths(item, current_path, result)
  29. else:
  30. # 如果是终节点,将当前路径和值保存到字典中
  31. if path not in ignore_data:
  32. result[path] = data
  33. return result
  34. def generate_dict_from_json(file_path):
  35. data = read_from_json(file_path)
  36. # 生成全路径的字典
  37. leaf_paths_dict = get_leaf_paths(data)
  38. return leaf_paths_dict
  39. def read_data_from_json(oem, lang):
  40. # C:\Workspaces\Code_Git\MTP2.0_NEWMANAGE_WEB\public\locales\zh-CN.json
  41. common_file = 'C:/Workspaces/Code_Git/MTP2.0_NEWMANAGE_WEB\public/locales/' + lang + '.json'
  42. common_data = generate_dict_from_json(common_file)
  43. '''
  44. 管理后台暂无OEM
  45. # C:\Workspaces\Code_Git\MTP2.0_NEWMANAGE_WEB\oem\tss\locales\extras\zh-CN.json
  46. oem_file = 'C:/Workspaces/Code_Git/MTP20_WEB_GLOBAL/oem/' + \
  47. oem + '/locales/extras/' + lang + '.json'
  48. oem_data = generate_dict_from_json(oem_file)
  49. if oem_data:
  50. for key, value in oem_data.items():
  51. # 更新通用字典的oem个性化值(有则更新,无则添加)
  52. common_data[key] = value
  53. '''
  54. return common_data
  55. def generate_excle_by_oem(oem):
  56. zh_data = read_data_from_json(oem, 'zh-CN')
  57. en_data = read_data_from_json(oem, 'en-US')
  58. th_data = read_data_from_json(oem, 'th-TH')
  59. tw_data = read_data_from_json(oem, 'zh-TW')
  60. df = pd.DataFrame.from_dict(zh_data, orient='index', columns=['zh-CN'])
  61. # 将 其它 的值合并到 DataFrame 中
  62. df['en-US'] = df.index.map(en_data).fillna('')
  63. df['th-TH'] = df.index.map(th_data).fillna('')
  64. df['zh-TW'] = df.index.map(tw_data).fillna('')
  65. # 重置索引,以便将索引变为一列
  66. df.reset_index(inplace=True)
  67. df.rename(columns={'index': 'Key'}, inplace=True) # 将索引列重命名为 'Key'
  68. # 导出到 Excel 文件
  69. output_file = output_folder + '/excels/' + oem + '.xlsx'
  70. df.to_excel(output_file, index=False)
  71. # 使用 openpyxl 设置列宽
  72. wb = load_workbook(output_file)
  73. ws = wb.active
  74. # 指定列宽
  75. ws.column_dimensions['A'].width = 60 # 设置第一列宽度
  76. ws.column_dimensions['B'].width = 50 # 设置第二列宽度
  77. ws.column_dimensions['C'].width = 50 # 设置第三列宽度
  78. ws.column_dimensions['D'].width = 50 # 设置第四列宽度
  79. ws.column_dimensions['E'].width = 50 # 设置第五列宽度
  80. # 保存更改
  81. wb.save(output_file)
  82. print("字典已成功输出到 " + output_file)
  83. def update_excel_by_inc(oem, inc_name):
  84. ''' 增量更新,在生成excel里标注新增的key为黄色 '''
  85. # 读取输出的Excel文件, 格式:key(A) zh-CN(B) en-US(C) th-TH(C) zh-TW(D)
  86. inc_file = output_folder + "/excels/" + inc_name + ".xlsx"
  87. if not os.path.exists(inc_file):
  88. return
  89. df = pd.read_excel(inc_file)
  90. # 将 A 列和 C 列转换为zh-CN字典
  91. old_dic = dict(zip(df.iloc[:, 0], df.iloc[:, 1]))
  92. new_file = output_folder + "/excels/" + oem + ".xlsx"
  93. wb = load_workbook(new_file)
  94. sheet = wb.active #
  95. # 定义黄色填充样式
  96. yellow_fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid")
  97. # 遍历第一列
  98. for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row, min_col=1, max_col=1):
  99. cell = row[0]
  100. if cell.value not in old_dic:
  101. cell.fill = yellow_fill
  102. # 保存文件
  103. update_file = output_folder + "/excels/" + oem + "_inc.xlsx"
  104. wb.save(update_file) # 保存为新文件,避免覆盖原文件
  105. print("增量字典已成功输出到 " + update_file)
  106. if __name__ == '__main__':
  107. # 根据代码json地址生成excel
  108. generate_excle_by_oem('newmanage')
  109. # 增量更新,在生成excel里标注新增的key为黄色
  110. update_excel_by_inc('newmanage', '20241201_newmanage_inc')