#-*-coding:utf-8-*-
#-*-coding:big5-*-#-*-coding:cp936-*-import Tkinterimport tkFileDialogimport osimport tkMessageBoximport shutilurlnew=""#文件打开def manipuOpen(): url = tkFileDialog.askopenfilename(title = '打开html文件', #创建打开文件对话框 filetypes=[('HTML', '*.htm'), ('All files', '*')] ) #指定文件类型 return url def FileOpen(): global urlnew url=manipuOpen()#得到要操作的文件路径 urltemp=url[:-4]+"temp"+url[-4:] shutil.copy2(url,urltemp) urlnew=urltemp[:-3]+"htm" os.rename(urltemp,urlnew)#文件保存def manipuSave(): url= tkFileDialog.asksaveasfilename(title = '保存生成的代码', #创建保存文件对话框 filetypes=[('文本文件', '*.txt'), ('All files', '*')]) return urldef FileSave(): global urlnew urlsave=manipuSave() GenerateCode(urlnew,urlsave)#生成代码def GenerateCode(oldString,newString): SourceFile=open(oldString,"r") string="" lines=SourceFile.readlines() for i in range(0,len(lines)): lines[i]=lines[i][:-1]+"\\n" string=string+str(lines[i]) string=string.replace(r'"',r'\"') DestFile=open(newString,"w") DestFile.write(string) tkMessageBox.showinfo(message="导出成功!") #生成代码def main(): root = Tkinter.Tk() root.resizable(0,0) root.title('htm代码生成器') button1 = Tkinter.Button(root, text = '打开html文件', #创建按钮 command = FileOpen) button1.pack(side='left') button2 = Tkinter.Button(root, text = '保存生成的代码', command = FileSave) button2.pack(side='left') root.mainloop()main()