读取xlsx文件,生成string文件

77 阅读1分钟

import pandas as pd

读取xlsx文件

file_path = '/Users/dhf/Desktop/多语言翻译.xlsx' # .xlsx 文件路径 df = pd.read_excel(file_path)

指定Key和Value所在的列,假设分别在A列和B列

key_column = 'ios_key(key不要更改)' # 替换为实际的列名或索引 value_column = '繁体' # 替换为实际的列名或索引

创建strings文件内容

output = '' for index, row in df.iterrows(): key = row[key_column] value = row[value_column] # 确保key和value都是字符串并且替换掉双引号 key = str(key).replace('"', '"') value = str(value).replace('"', '"') output += f'"{key}" = "{value}";\n'

将内容写入strings文件

output_file_path = r"/Users/dhf/Desktop/testOc/testOc/zh-HK.lproj/Localizable.strings"

with open(output_file_path, 'w', encoding='utf-8') as f: f.write(output)

print(f'已生成 {output_file_path} 文件')