48 lines
2.0 KiB
Python
48 lines
2.0 KiB
Python
# file_path = r"C:\Users\Administrator.DESKTOP-7IC2USJ\Desktop\ARMASTลูกค้า 3.txt"
|
|
# output_file_path = r"C:\Users\Administrator.DESKTOP-7IC2USJ\Desktop\ARMASTลูกค้า 215415.txt"
|
|
#
|
|
# # 使用TIS-620编码读取文件,并处理可能的编码错误
|
|
# with open(file_path, 'r', encoding='TIS-620', errors='replace') as file:
|
|
# content = file.read()
|
|
#
|
|
# # 直接将内容写入新的UTF-8编码文件
|
|
# with open(output_file_path, 'w', encoding='UTF-8') as output_file:
|
|
# output_file.write(content)
|
|
#
|
|
import pandas as pd
|
|
df = pd.read_excel(r"C:\Users\Administrator.DESKTOP-7IC2USJ\Desktop\新建文件夹\tai.xlsx",sheet_name="Products")
|
|
|
|
output_file_path = r'C:\Users\Administrator.DESKTOP-7IC2USJ\Desktop\新建文件夹\translated_excel_file.csv' # 替换为你想要保存的文件路径
|
|
df.to_csv(output_file_path)
|
|
print(f"File has been converted and saved to {output_file_path}")
|
|
|
|
|
|
|
|
# import pandas as pd
|
|
# from googletrans import Translator
|
|
#
|
|
# # 初始化翻译器
|
|
# translator = Translator()
|
|
#
|
|
# # 读取Excel文件
|
|
# file_path = r"C:\Users\Administrator.DESKTOP-7IC2USJ\Desktop\新建文件夹\tai.xlsx" # 替换为你的Excel文件路径
|
|
# df = pd.read_excel(file_path,sheet_name='Products',engine='openpyxl')
|
|
#
|
|
# # 定义一个函数来翻译单元格内容
|
|
# def translate_text(text, src='zh-cn', dest='th'):
|
|
# try:
|
|
# translated = translator.translate(text, src=src, dest=dest)
|
|
# return translated.text
|
|
# except Exception as e:
|
|
# print(f"Error translating text '{text}': {e}")
|
|
# return text
|
|
#
|
|
# # 遍历DataFrame并翻译每个单元格的内容
|
|
# for column in df.columns:
|
|
# df[column] = df[column].apply(lambda x: translate_text(str(x)) if isinstance(x, str) else x)
|
|
#
|
|
# # 保存翻译后的DataFrame到新的Excel文件
|
|
# output_file_path = r'C:\Users\Administrator.DESKTOP-7IC2USJ\Desktop\新建文件夹\translated_excel_file.xlsx' # 替换为你想要保存的文件路径
|
|
# df.to_excel(output_file_path, index=False)
|
|
#
|
|
# print("Translation complete and saved to", output_file_path) |