代码改变世界

html字符串生成器源代码

2012-03-12 15:49  Python爱好者  阅读(629)  评论(0编辑  收藏  举报

#-*-coding:utf-8-*-
#-*-coding:big5-*-
#-*-coding:cp936-*-
import Tkinter
import tkFileDialog
import os
import tkMessageBox
import shutil
urlnew=""
#文件打开
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 url
def 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()