英文转中文十六进制文本翻译器
这个主要是用在软件汉化时辅助翻译,英文字节和中文字节长度不一样使用~
from tkinter import *
import tkinter as tk
from urllib import request
from urllib import parse
import json
import hashlib
import random
import struct,re,time,pyperclip
window.title('英文转中文16进制文本翻译器')
window.geometry('530x600')
def translate_Word(en_str):
URL = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
Form_Data = {}
Form_Data['from'] = 'en'
Form_Data['to'] = 'zh'
Form_Data['q'] = en_str #要翻译数据
Form_Data['transtype'] = 'hash'
Form_Data['appid'] = '' #这里输入您申请的APP ID
Form_Data['salt'] = str(random.randint(32768, 65536)) #随机数
Key="" #这里输入平台分配的密钥
m=Form_Data['appid']+en_str+Form_Data['salt']+Key
m_MD5 = hashlib.md5(m.encode('utf8'))
Form_Data['sign'] = m_MD5.hexdigest()
data = parse.urlencode(Form_Data).encode('utf-8') #使用urlencode方法转换标准格式
response = request.urlopen(URL,data) #传递Request对象和转换完格式的数据
html = response.read().decode('utf-8') #读取信息并解码
translate_results = json.loads(html) #使用JSON
print(translate_results) #打印出JSON数据
translate_results = translate_results['trans_result']
s=[]
for i in translate_results:
translate_results = i['dst']
s.append(translate_results)
return s
# 是文本数据始终停留在最后一行
def end_():
hex_data.see(tk.END)
hex_data.update()
# 复制结果到粘贴板
def copyText(key):
value = pyperclip.copy(key)
return value
# 清空所有数据
def dele():
yiwen.delete(0.0,'end')
yuanwen.delete(0.0,'end')
hex_data.delete(0.0,'end')
jieguo.set("")
yuanwen_hex_zijie.set("")
yuanwen_zijie.set("")
yiwen_zijie.set("")
yiwen_hex_zijie.set("")
xiangcha_zijie.set("")
jieguo_zijie.set("")
jieguo_hex_zijie.set("")
yuanwen.focus_force()
# 中文字符串转16进制编辑后输出
def utf_to_utf(data_txt):
yiwen_data=data_txt
yiwen_by=yiwen_data.encode('gbk')
yi_lenn=len(yiwen_data.encode('gbk'))-2
a=[]
for i in yiwen_by:
a.append(i)
aas=a[:yi_lenn]
d=struct.pack('<'+str(yi_lenn)+'B',*aas)
gbk_to_utf8=d.decode("gbk").encode("utf-8")
utf8_decode = gbk_to_utf8.decode("utf-8")
return utf8_decode
# 格式化HEX数据
def hex_kong(text_d):
b=str.upper(text_d)
text_list = re.findall(".{2}",b)
text = " ".join(text_list)
text_len=len(text)
new_text=text[:text_len]
return new_text
xiangjian_len=''
# 翻译
def fanyi():
global xiangjian_len
yiwen.delete(0.0,'end')
aass=0
while True:
try:
yuanwen_data=yuanwen.get("0.0","end")
ss=translate_Word(yuanwen_data)
aass=0
break
except KeyError:
aass+=1
if aass>10:
tt=tk.messagebox.showinfo(title='提示', message='翻译失败请重新更换要翻译的文本或重启软件!!') # return 'ok'
aass=0
break
aas=0
try:
for i in ss:
aas+=1
yiwen.insert('end',i)
# # if aas==2:
yiwen.insert('end','\n')
except UnboundLocalError:
print('翻译失败.')
# 打印数据
def Print():
hex_data.delete(0.0,'end')
yuanwen_data=yuanwen.get("0.0","end")
yuanwen_len=len(yuanwen_data.encode('gbk'))-1
yuanwen_by=yuanwen_data.encode('gbk')
yuanwen_hexstring=yuanwen_by.hex()
yuanwen_hex_len=len(yuanwen_hexstring.encode('gbk'))-2
yuanwen_hex=yuanwen_hexstring[:yuanwen_hex_len]
yuanwen_hex_data=hex_kong(yuanwen_hex)
yiwen_data=yiwen.get("0.0","end")
yiwen_len=len(yiwen_data.encode('gbk'))-2
yiwen_by=yiwen_data.encode('gbk')
yiwen_hexstring=yiwen_by.hex()
yiwen_hex_len=len(yiwen_hexstring.encode('gbk'))-2
yiwen_hex=yiwen_hexstring[:yiwen_hex_len]
yiwen_hex_data=hex_kong(yiwen_hex)
yuanwen_zijie.set(yuanwen_len)
yuanwen_hex_zijie.set(yuanwen_hex_len)
yiwen_zijie.set(yiwen_len)
yiwen_hex_zijie.set(yiwen_hex_len)
xiangjian_len=yuanwen_len-yiwen_len
jieguo.set(utf_to_utf(yiwen_data)+" "*xiangjian_len)
jieguo_data=jieguo.get()
jieguo_len=len(jieguo_data.encode('gbk'))
jieguo_by=jieguo_data.encode('gbk')
jieguo_hexstring=jieguo_by.hex()
jieguo_1=str.upper(jieguo_hexstring)
jieguo_text = re.findall(".{2}",jieguo_1)
jieguo_hex = " ".join(jieguo_text)
jieguo_hex_len=len(jieguo_hexstring.encode('gbk'))
jieguo_zijie.set(jieguo_len)
jieguo_hex_zijie.set(jieguo_hex_len)
copyText(jieguo_data)
xiangcha_zijie.set(xiangjian_len)
if xiangjian_len<0:
tt=tk.messagebox.showinfo(title='提示', message='输出字符超出原始字符,请修改!!') # return 'ok'
hex_data.insert('end'," "*15+">"*6+" "+"原文16进制数据"+" "+"<"*6+"\n"+"="*60)
hex_data.insert('end',yuanwen_hex_data)
hex_data.insert('end','\n\n\n')
end_()
hex_data.insert('end'," "*15+">"*6+" "+"译文16进制数据"+" "+"<"*6+"\n"+"="*60)
hex_data.insert('end',yiwen_hex_data)
hex_data.insert('end','\n\n\n')
end_()
hex_data.insert('end'," "*15+">"*6+" "+"结果16进制数据"+" "+"<"*6+"\n"+"="*60)
hex_data.insert('end',jieguo_hex)
end_()
# 原文输入框
tk.Label(window, text='输入需要翻译的文字').place(x=230, y=1, anchor='nw')
yuanwen=tk.Text(window,height=10,width=60)
yuanwen.place(x=100, y=20, anchor='nw')
# 译文显示框
tk.Label(window, text='翻译结果').place(x=250, y=160, anchor='nw')
yiwen=tk.Text(window,height=10,width=60)
yiwen.place(x=100, y=180, anchor='nw')
# 十六进制显示框
tk.Label(window, text='十六进制数据显示区').place(x=230, y=320, anchor='nw')
hex_data=tk.Text(window,height=16,width=60)
hex_data.place(x=100, y=340, anchor='nw')
# 结果显示框
tk.Label(window, text='输出结果:').place(x=2, y=560, anchor='nw')
jieguo=tk.StringVar()
tk.Entry(window,textvariable =jieguo ,show=None,width=66).place(x=60,y=560,anchor='nw')
# 翻译
tk.Button(window,text='翻译',command=fanyi,width=10).place(x=2,y=36,anchor='nw')
# 清空
tk.Button(window,text='清空',command=dele,width=10).place(x=2,y=86,anchor='nw')
# 打印结果
tk.Button(window,text='打印',command=Print,width=10).place(x=2,y=146,anchor='nw')
# tk.Button(window,text='test',command=tks,width=10).place(x=2,y=246,anchor='nw')
# 原文字节
tk.Label(window, text='原文字节:').place(x=1, y=350, anchor='nw')
yuanwen_zijie=tk.StringVar()
tk.Entry(window,textvariable =yuanwen_zijie ,show=None,width=4).place(x=60,y=350,anchor='nw')
# 原文HEX
tk.Label(window, text='原文HEX:').place(x=1, y=380, anchor='nw')
yuanwen_hex_zijie=tk.StringVar()
tk.Entry(window,textvariable =yuanwen_hex_zijie ,show=None,width=4).place(x=60,y=380,anchor='nw')
# 译文字节
tk.Label(window, text='译文字节:').place(x=1, y=410, anchor='nw')
yiwen_zijie=tk.StringVar()
tk.Entry(window,textvariable =yiwen_zijie ,show=None,width=4).place(x=60,y=410,anchor='nw')
# 译文HEX
tk.Label(window, text='译文HEX:').place(x=1, y=440, anchor='nw')
yiwen_hex_zijie=tk.StringVar()
tk.Entry(window,textvariable =yiwen_hex_zijie ,show=None,width=4).place(x=60,y=440,anchor='nw')
# 相差
tk.Label(window, text='相差字节::').place(x=1, y=470, anchor='nw')
xiangcha_zijie=tk.StringVar()
tk.Entry(window,textvariable =xiangcha_zijie ,show=None,width=4).place(x=60,y=470,anchor='nw')
# 结果字节
tk.Label(window, text='结果字节:').place(x=1, y=500, anchor='nw')
jieguo_zijie=tk.StringVar()
tk.Entry(window,textvariable =jieguo_zijie ,show=None,width=4).place(x=60,y=500,anchor='nw')
# 结果HEX
tk.Label(window, text='结果HEX:').place(x=1, y=530, anchor='nw')
jieguo_hex_zijie=tk.StringVar()
tk.Entry(window,textvariable =jieguo_hex_zijie ,show=None,width=4).place(x=60,y=530,anchor='nw')
window.mainloop()
运行结果: