generate_excle.py 4.6 KB

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