def __init__(self, master): tk.Frame.__init__(self, master=master) title1_2 = tk.Label(master=self, text="新Excel:") new_excel = ui.FileOpenUI(master=self, file_types=[("Excel File", "*.xlsx")], base_root=lambda: config.excel_root(), title=u"翻译文件") # file1_2 = tk.StringVar(value = "") # input1_2 = tk.Entry(master=self, textvariable=file1_2) # btn1_2 = tk.Button(master=self, text="打开", command=lambda : viewtool.set_json_dir(self.winfo_toplevel(), file1_2, filetypes=[("Excel File", "*.xlsx")], title="翻译文件", initialdir=config.excel_root())) title2_2 = tk.Label(master=self, text="旧Excel:") file2_2 = tk.StringVar(value="") input2_2 = tk.Entry(master=self, textvariable=file2_2) btn2_2 = tk.Button(master=self, text="打开", command=lambda: viewtool.set_json_dir( self.winfo_toplevel(), file2_2, filetypes=[("Excel File", "*.xlsx")], title="翻译文件", initialdir=config.excel_root())) pickbtn = tk.Button( master=self, text="提取", command=lambda: self.diff_language(new_excel.get_value(), file2_2)) row = 0 title1_2.grid(row=row, column=0, columnspan=6, sticky=tk.W) row += 1 new_excel.grid(row=row, column=0, columnspan=6, sticky=tk.EW) # btn1_2.grid(row = row, column = 6, sticky = tk.EW, padx = 5) row += 1 title2_2.grid(row=row, column=0, columnspan=6, sticky=tk.W) row += 1 input2_2.grid(row=row, column=0, columnspan=6) btn2_2.grid(row=row, column=6, sticky=tk.EW, padx=5) row += 1 pickbtn.grid(row=row, column=0, sticky=tk.W)
def slim_language(self, file1, file2): excel1 = file1.get() jsonfilename = file2.get() wb = openpyxl.load_workbook(excel1, read_only=True) ws = wb[wb.sheetnames[0]] dict1 = translateapi.excel_to_dict(ws) wb.close() with open(jsonfilename, "r", encoding='UTF-8') as f: jdata = json.load(f) lanlist = jdata['data'] cnset = translateapi.create_cn_set(lanlist) newdict = {} for k in cnset.keys(): cn = translateapi.escape_string(k) lan = dict1[cn] if cn in dict1 else None newdict[cn] = lan wb = openpyxl.Workbook() ws = wb.active ws.cell(1, 1).value = u"中文" ws.cell(1, 2).value = u"译文" row = 2 for k, v in newdict.items(): ws.cell(row, 1).value = k ws.cell(row, 2).value = v row += 1 (_, file_name) = os.path.split(excel1) (short_name, _) = os.path.splitext(file_name) save_dir = Dialog.asksaveasfilename(master=self.master, filetypes=[("Excel File", "*.xlsx") ], title="请选择保存Excel文件的路径", initialdir=config.excel_root(), initialfile=short_name) if save_dir == "": return (save_dir, file_name) = os.path.split(save_dir) (short_name, _) = os.path.splitext(file_name) save_full_name = os.path.join(save_dir, short_name + ".xlsx") wb.save(save_full_name) wb.close()
def __init__(self, master): tk.Frame.__init__(self, master=master) title1_2 = tk.Label(master=self, text="Excel:") file1_2 = tk.StringVar(value="") input1_2 = tk.Entry(master=self, textvariable=file1_2, width=80) btn1_2 = tk.Button(master=self, text="打开", command=lambda: viewtool.set_json_dir( self.winfo_toplevel(), file1_2, filetypes=[("Excel File", "*.xlsx")], title="翻译文件", initialdir=config.excel_root())) title2_2 = tk.Label(master=self, text="Json:") file2_2 = tk.StringVar(value="") input2_2 = tk.Entry(master=self, textvariable=file2_2, width=80) btn2_2 = tk.Button(master=self, text="打开", command=lambda: viewtool.set_json_dir( self.winfo_toplevel(), file2_2, filetypes=[("Json File", "*.json")], title="中文json", initialdir=config.json_root())) pickbtn = tk.Button( master=self, text="清理", command=lambda: self.slim_language(file1_2, file2_2)) row = 0 title1_2.grid(row=row, column=0, columnspan=6, sticky=tk.W) row += 1 input1_2.grid(row=row, column=0, columnspan=6) btn1_2.grid(row=row, column=6, sticky=tk.EW, padx=5) row += 1 title2_2.grid(row=row, column=0, columnspan=6, sticky=tk.W) row += 1 input2_2.grid(row=row, column=0, columnspan=6) btn2_2.grid(row=row, column=6, sticky=tk.EW, padx=5) row += 1 pickbtn.grid(row=row, column=0, sticky=tk.W)
def diff_language(self, file1, file2): excel1 = file1.get() excel2 = file2.get() wb = openpyxl.load_workbook(excel1, read_only=True) ws = wb[wb.sheetnames[0]] dict1 = excel_to_dict(ws) wb.close() wb = openpyxl.load_workbook(excel2, read_only=True) ws = wb[wb.sheetnames[0]] dict2 = excel_to_dict(ws) wb.close() newdict = {} for k, v in dict1.items(): if not k in dict2 or dict2[k] != v: newdict[k] = v wb = openpyxl.Workbook() ws = wb.active ws.cell(1, 1).value = u"中文" ws.cell(1, 2).value = u"译文" row = 2 for k, v in newdict.items(): ws.cell(row, 1).value = k ws.cell(row, 2).value = v row += 1 (_, file_name) = os.path.split(excel1) (short_name, _) = os.path.splitext(file_name) save_dir = Dialog.asksaveasfilename(master=self.master, filetypes=[("Excel File", "*.xlsx") ], title="请选择保存Excel文件的路径", initialdir=config.excel_root(), initialfile=short_name) if save_dir == "": return (save_dir, file_name) = os.path.split(save_dir) (short_name, _) = os.path.splitext(file_name) save_full_name = os.path.join(save_dir, short_name + ".xlsx") wb.save(save_full_name) wb.close()
def __init__(self, parent): tkinter.Frame.__init__(self, master = parent) root = self.winfo_toplevel() title2_1 = tkinter.Label(master=self, text = u"待翻译json文件:") file2_1 = tkinter.StringVar(value = "") self._jsonfile = file2_1 input2_1 = tkinter.Entry(master=self, textvariable=file2_1) viewtool.bind_drop(input2_1) command = lambda : viewtool.set_json_dir(root, file2_1, filetypes=[("Json File", "*.json")], title=u"待翻译json", initialdir=config.json_root()) btn2_1 = tkinter.Button(master=self, text=u"打开", command=command, width = 10) row = 0 title2_1.grid(row = row, column = 0, sticky = tkinter.W) row += 1 input2_1.grid(row = row, column = 0, columnspan = 6, sticky = tkinter.EW) btn2_1.grid(row = row, column = 6, sticky = tkinter.EW, padx = 5) title2_2 = tkinter.Label(master=self, text = u"新译文:") file2_2 = tkinter.StringVar(value = "") self._excelfilenew = file2_2 input2_2 = tkinter.Entry(master=self, textvariable=file2_2, width = 80) viewtool.bind_drop(input2_2) command = lambda : viewtool.set_json_dir(root, file2_2, filetypes=[("Excel File", "*.xlsx")], title=u"新译文", initialdir=config.excel_root()) btn2_2 = tkinter.Button(master=self, text=u"打开", command=command, width = 10) row += 1 title2_2.grid(row = row, column = 0, sticky = tkinter.W) row += 1 input2_2.grid(row = row, column = 0, columnspan = 6, sticky = tkinter.EW) btn2_2.grid(row = row, column = 6, sticky = tkinter.EW, padx = 5) title3_2 = tkinter.Label(master=self, text = u"旧译文:(可选,假如存在,则会把json中的旧译文替换成新译文)") file3_2 = tkinter.StringVar(value = "") self._excelfileold = file3_2 input3_2 = tkinter.Entry(master=self, textvariable=file3_2, width = 80) viewtool.bind_drop(input3_2) command = lambda : viewtool.set_json_dir(root, file3_2, filetypes=[("Excel File", "*.xlsx")], title=u"旧译文", initialdir=config.excel_root()) btn3_2 = tkinter.Button(master=self, text=u"打开", command=command, width = 10) row += 1 title3_2.grid(row = row, column = 0, sticky = tkinter.W, columnspan = 6) row += 1 input3_2.grid(row = row, column = 0, columnspan = 6, sticky = tkinter.EW) btn3_2.grid(row = row, column = 6, sticky = tkinter.EW, padx = 5) transbtn = tkinter.Button(master=self, text=u"翻译", command=self.translate) row += 1 transbtn.grid(row = row, column = 2, sticky = tkinter.EW)
def pick_cn(self): root = self.winfo_toplevel() jsonfilename = self._jsonfile.get() if jsonfilename == "" or not os.path.isfile(jsonfilename): sytlog.log(u"没有选择json文件\n") MsgBox.showerror(u"错误", u"请选择一个json文件") return else: sytlog.log(u"开始提取\n") with open(jsonfilename, "r", encoding='UTF-8') as f: jdata = json.load(f) lanlist = jdata['data'] cnset = translateapi.create_cn_set(lanlist) if len(cnset) > 0: excelfilename = self._excelfile.get() if excelfilename != "" and os.path.isfile(excelfilename): wb1 = openpyxl.load_workbook(excelfilename, read_only = True) dict1 = translateapi.excel_to_dict(wb1[wb1.sheetnames[0]]) wb1.close() set1 = set() for k in dict1: set1.add(translateapi.invert_escape_string(k)) cnset = set(cnset) - set1 if len(cnset) == 0: sytlog.log(u"没有可提取的中文\n") return wb = translateapi.create_lan_excel_from_map(cnset) (_, file_name) = os.path.split(jsonfilename) (short_name, _) = os.path.splitext(file_name) save_dir = Dialog.asksaveasfilename(master = root, filetypes=[("Excel File", "*.xlsx")], title = "请选择保存Excel文件的路径", initialdir = config.excel_root(), initialfile = short_name + u".xlsx") if save_dir == "": sytlog.log(u'取消\n') return (save_dir, file_name) = os.path.split(save_dir) (short_name, _) = os.path.splitext(file_name) save_full_name = os.path.join(save_dir, short_name + ".xlsx") wb.save(save_full_name) wb.close() sytlog.log(u"提取完成\n") else: sytlog.log(u"没有可提取的中文\n")
def __init__(self, parent): tkinter.Frame.__init__(self, master = parent) title1_1 = tkinter.Label(master=self, text = u"提取json源文件:") file1_1 = tkinter.StringVar(value = "") input1_1 = tkinter.Entry(master=self, textvariable=file1_1, width = 80) viewtool.bind_drop(input1_1) command = lambda : viewtool.set_json_dir(self.winfo_toplevel(), file1_1, filetypes=[("Json File", "*.json")], title=u"文件源json", initialdir=config.json_root()) btn1_1 = tkinter.Button(master=self, text=u"打开", command=command, width = 10) title1_2 = tkinter.Label(master=self, text = u"排除译文(可选,设置该路径将排除掉该Excel中已有的中文):") file1_2 = tkinter.StringVar(value = "") input1_2 = tkinter.Entry(master=self, textvariable=file1_2, width = 80) viewtool.bind_drop(input1_2) command = lambda : viewtool.set_json_dir(self.winfo_toplevel(), file1_2, filetypes=[("Excel File", "*.xlsx")], title=u"翻译文件", initialdir=config.excel_root()) btn1_2 = tkinter.Button(master=self, text=u"打开", command=command, width = 10) pickbtn = tkinter.Button(master=self, text=u"提取", command=self.pick_cn) row = 0 title1_1.grid(row = row, column = 0, sticky = tkinter.W) row += 1 input1_1.grid(row = row, column = 0, columnspan = 6, sticky = tkinter.EW) btn1_1.grid(row = row, column = 10, sticky = tkinter.EW, padx = 5) row += 1 title1_2.grid(row = row, column = 0, columnspan = 6, sticky = tkinter.W) row += 1 input1_2.grid(row = row, column = 0, columnspan = 6, sticky = tkinter.EW) btn1_2.grid(row = row, column = 10, sticky = tkinter.EW, padx = 5) row += 1 pickbtn.grid(row = row, column = 2, sticky = tkinter.EW) self._jsonfile = file1_1 self._excelfile = file1_2