需求: vue快速实现国际化文案key替换
py代码快速批量检索国际化所需的value值并输出到Excel文件
py代码
import os
import re
import pandas as pd
from openpyxl import load_workbook
def extract_chinese_from_file(file_path):
readData = {'file': [], 'chineseText': []}
if not file_path.endswith(('.js')):
return readData
with open(file_path, 'r', encoding='utf-8') as file:
text = file.read()
chinese_pattern = re.compile('[\u4e00-\u9fa5]+')
chinese_matches = chinese_pattern.findall(text)
if chinese_matches:
print(f"File: {file_path}")
for chinese_text in chinese_matches:
readData['chineseText'].append(chinese_text)
readData['file'].append(file_path)
else:
print(f"No Chinese text found in file: {file_path}")
return readData
def extract_chinese_from_directory(directory_path):
data = {'file': [], 'chineseText': []}
for filename in os.listdir(directory_path):
file_path = os.path.join(directory_path, filename)
if os.path.isfile(file_path):
print(f"Checking file: {file_path}")
readData = extract_chinese_from_file(file_path)
if readData is not None:
data['chineseText'].extend(readData['chineseText'])
data['file'].extend(readData['file'])
else:
readData = extract_chinese_from_directory(file_path)
if readData is not None:
data['chineseText'].extend(readData['chineseText'])
data['file'].extend(readData['file'])
return data
# 指定目录路径
target_directory = []
target_directory.append("/Users/chenchong/IdeaProjects/gwm-swip/iov-foundation-web/src/views")
# 提取中文并打印
for dir in target_directory:
data = extract_chinese_from_directory(dir)
# 将结果写入到 Excel 文件
last_part = os.path.basename(dir)
output_excel_path = last_part + ".xlsx"
df = pd.DataFrame(data)
df.to_excel(output_excel_path, index=False)
print(f"Results exported to: {output_excel_path}")