python脚本-Gui选取excel文件并读取入库

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xlrd, tkFileDialog

def insertdb(list):
    #appConn = MySQLdb.connect(host='xx.xx.xx.xx', user='root', passwd='123456', db='xxx_db', port=3306)
    #appConn.set_character_set('utf8')
    #appCur = appConn.cursor()
    #appCur.execute('SET NAMES utf8;')
    #appCur.execute('SET CHARACTER SET utf8;')
    #appCur.execute('SET character_set_connection=utf8;')
    for i in list:
        print i
        #appCur.execute("""xxxxxx""")

def getvalue(filename):
    data = xlrd.open_workbook(filename)
    #sheetname = data.sheet_names()
    sheet = data.sheet_by_index(0)
    rows = sheet.nrows
    cols = sheet.ncols
    #print cols
    valuelist = []
    for row in range(1, rows - 1):
        value = sheet.row_values(row)
        #print value
        valuelist.append(value)
    print valuelist
    insertdb(valuelist)

def guirun():

    initialdir = '/Users/cescwu/Downloads/'
    #initialdir = 'd:/'
    filename = tkFileDialog.askopenfilename(initialdir = initialdir, filetypes=[("Text files","*.xlsx")], message = '请选择需要入库的文件')
    print filename
    getvalue(filename)

if __name__ == '__main__':
    guirun()

主要用到python读取excel文件的xlrd插件,还有tkFileDialog模块,入库的步骤省略了。

另外,可以使用pyinstaller将python脚本打包为exe文件。

下载:https://github.com/pyinstaller/pyinstaller/releases/download/3.0/PyInstaller-3.0.tar.gz

解压后安装模块

python setup.py install

将脚本放到解压目录下,运行python pyinstaller.py -F scripts.py即可。

发表评论

电子邮件地址不会被公开。 必填项已用*标注