經常用python寫一些小工具,一段時間不用後就不知道放哪兒去了,即使找出來也不記得它是幹啥的??@-@
用PyQt把它們集合到一個桌面小工具裏,再配上工具說明和應用場景,嗯,這回應該不會弄丟了。
基本知識參見:
QTDesinger 和PyUIC的安裝和使用參見前文《從零開始 使用PyQt5》
第一個簡單程序《PyQt GUI編程——猜數字》
___________________________________________________________________________________
一、工具設計
第一個小工具:excel 表格轉 mysql insert語句
功能:在excel中開發的數據庫內容轉換爲 mysql insert語句,copy+paste就可以更新數據庫內容啦。
界面中應包含:工具說明 + 應用場景 + 輸入文件(excel)選擇 並顯示在界面上+ 輸出路徑選擇 並顯示在界面上+ 啓動按鈕+ 成功/失敗提示。
二、建立新工程
1、PyCharm中建立新工程:pythonDeskTool,工程設置沿用《PyQt GUI編程——猜數字》的設置。
2、配置interpreter :進入 File/settings/Project:pythonDeskTool/Project Interpreter 右側點擊加號(+)安裝pyqt5, pyqt5-sip,pyqt5-tools。
三、生成工具界面
3、工程目錄下新建 deskTool目錄。選擇Tools/External Tools/QTdesigner 進入圖形界面編輯器,新建desktool.ui 如下圖
4、右鍵點擊desktool.ui ,彈出菜單中選擇External Tools->PyUIC 轉換生成 desktool.py。desktool.py文件移入deskTool目錄。
(由於工具配置問題,desktool.ui 必須位於工程根目錄下)
5、deskTool下新建toolMain.py文件
# -*- coding: utf-8 -*-
"""python桌面工具集合"""
from PyQt5 import QtWidgets # 導入PyQt5部件
import sys
from deskTool import Ui_MainWindow
app = QtWidgets.QApplication(sys.argv) # 建立application對象
first_window = Ui_MainWindow() # 建立窗體對象
first_window.show() # 顯示窗體
sys.exit(app.exec()) # 運行程序
6、修改desktool.py文件
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Ui_MainWindow(QMainWindow):
def __init__(self, parent=None):
super(Ui_MainWindow, self).__init__(parent)
self.setupUi(self)
self.retranslateUi(self)
def setupUi(self, MainWindow):
7、運行toolMain.py,成功彈出工具界面。
四:選擇並顯示 輸入文件 + 輸出路徑
使用tkinter庫實現文件或目錄的選擇。tkinter庫在安裝python時自帶,只需import即可。這部分修改全部在desktool.py中
1、引入tkinter庫
from PyQt5.QtWidgets import *
import tkinter as tk
from tkinter import filedialog
2、選擇並顯示輸入文件:
1)綁定信號和槽:
def signal_slot(self, MainWindow):
self.input_button.clicked.connect(self.get_input_file)
def __init__(self, parent=None):
super(Ui_MainWindow, self).__init__(parent)
self.setupUi(self)
self.retranslateUi(self)
self.signal_slot(self) # 綁定信號槽
2)選擇並顯示輸入文件
def __init__(self, parent=None):
super(Ui_MainWindow, self).__init__(parent)
self.setupUi(self)
self.retranslateUi(self)
self.signal_slot(self) # 綁定信號槽
root = tk.Tk()
root.withdraw()
def get_input_file(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
file_path = filedialog.askopenfilename()
self.input_file_path_label.setText(_translate("MainWindow", file_path))
3、選擇並顯示輸出路徑
1)綁定信號和槽:
def signal_slot(self, MainWindow):
self.input_button.clicked.connect(self.get_input_file)
self.output_path_button.clicked.connect(self.get_output_dir)
2)選擇並顯示輸出路徑
def get_output_dir(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
dir_path = filedialog.askdirectory()
self.output_path_label.setText(_translate("MainWindow", dir_path))
4、實現效果
由於沒有進行校驗,當前輸入文件可以是任何類型的文件。
五、點擊開始按鈕,開始轉換。
1、綁定信號和槽
def signal_slot(self, MainWindow):
# print('pushButton')
self.input_button.clicked.connect(self.get_input_file)
self.output_path_button.clicked.connect(self.get_output_dir)
self.start_button.clicked.connect(self.transform)
2、實現轉換
注意點:
1)工具使用的場景限制目前excel 表格名固定爲QuestionList ,SQL表名也是固定的。較好的方式是直接轉換excel表格名爲MySQL表名。
2)調試過程中發現表格內容爲數字時不能直接連接到字符串中,必須 浮點數->整數->字符串 後才能正確轉換。
3)生成insert語句時注意爲所有的字符串類型 添加" " 或' '。
4)注意符號的轉義,否則語句插入可能不成功。
def transform(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
input_file = self.input_file_path_label.text()
output_dir = self.output_path_label.text()
output_string = 'INSERT INTO level_estimate_recognize_words('
print("input file = " + input_file)
data = xlrd.open_workbook(input_file)
table = data.sheet_by_name('QuestionList') # 通過名稱獲取表格
print("table name = " + table.name)
nrows = table.nrows # 行數
ncols = table.ncols # 列數
row0 = table.row_values(0) # 第0行數據 數據庫表格的字段名
# 插入表字段名
for i in range(ncols):
output_string = output_string + row0[i]
if i != (ncols-1):
output_string = output_string + ','
else:
output_string = output_string + ')'
# print("output_string = " + output_string)
# 插入值
output_string = output_string + ' VALUES'
# print("output_string = " + output_string)
for i in range(1, nrows):
output_string = output_string + '('
# print("output_string = " + output_string)
for j in range(ncols):
cell_value = table.row(i)[j].value
cell_type = table.cell_type(i,j)
if cell_type == 2: # 數字
cell_value = str(int(cell_value))
if cell_type == 0: # 空
cell_value = '0'
if cell_type == 1: # 字符串
cell_value = '"' + cell_value + '"'
# print("cell_value = " + cell_value)
output_string = output_string + cell_value
# print("output_string = " + output_string)
if j != (ncols - 1):
output_string = output_string + ','
output_string = output_string + ')'
if i != (nrows - 1):
output_string = output_string + ','
localtime = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
output_file = output_dir + "/mysql_insert" + localtime + ".txt"
print("output_file = " + output_file)
f = open(output_file, 'w')
f.write(output_string)
f.close()
self.hint_label.setText(_translate("MainWindow", "完成!"))
六 、發佈軟件爲exe文件
1、安裝pyinstaller
進入 File/settings/Project:pythonDeskTool/Project Interpreter 右側點擊加號(+)安裝pyinstaller。
2、右鍵點擊要發佈的文件,這裏是toolMain.py。彈出菜單中選擇Open in Terminal
3、在編輯器下面的local 窗口中 輸入 pyinstaller -F toolMain.py 運行。運行完畢後,deskTool下出現dist文件夾,其中的toolMain.exe文件就是了。
4、上面方法生成的exe在運行時會彈出一個黑色cmd窗口,爲了取消。在local串口中輸入pyinstaller -F -w toolMain.py 運行。生成的exe 就不再有cmd窗口。
七、最終完成界面
---------------------------------------------------------------------------
That's all.Thank you!