workbook = load_workbook('学生信息.xlsx')
2.获得单元表Sheet1:
sheet = workbook['Sheet1']
3.获得指定单元格信息
#获得 1 行 1 列单元格
cell1 = sheet.cell(row=1, column=1)
4.项目代码(choose.py)
#随机抽取一名同学信息
from openpyxl import Workbook, load_workbook
import random
def ChooseStudent():
workbook = load_workbook('学生信息.xlsx')
sheet = workbook['Sheet1']
randomNum = random.randint(1,sheet.max_row - 1)
cell1 = sheet.cell(row=randomNum + 1, column=1)
cell2 = sheet.cell(row=randomNum + 1, column=2)
return (cell1.value,cell2.value)
5.补充运用
(1)新建excel:workbook1 = Workbook()
(2)保存excel:workbook1.save('1.xlsx')
(3)创建sheet:sheet = workword1.create_sheet(‘Sheet1',index=0)
(4)sheet最大(小)行列:sheet.max_row; sheet.min_row; sheet.max_column; sheet.min_column
(5)sheet每行(列)数据:sheet.rows; sheet.columns #返回每行(列)数据的一个tuple
例:(遍历excel所有数据)
for row in sheet.rows:
for cell in row:
print(cell.value)
(6)数据写入指定单元格:sheet.cell(row=1, column=2, value=3)
or sheet.cell(row=1, column=2) = 3
二、使用pyqt designer 设计窗口、控件
1.窗体效果
2.窗体设计代码(Windows.py)
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(544, 549)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButtonOK = QtWidgets.QPushButton(self.centralwidget)
self.pushButtonOK.setEnabled(True)
self.pushButtonOK.setGeometry(QtCore.QRect(185, 370, 171, 61))
font = QtGui.QFont()
font.setFamily("方正粗黑宋简体")
font.setPointSize(10)
self.pushButtonOK.setFont(font)
self.pushButtonOK.setCursor(QtGui.QCursor(QtCore.Qt.OpenHandCursor))
self.pushButtonOK.setAutoDefault(False)
self.pushButtonOK.setDefault(False)
self.pushButtonOK.setFlat(False)
self.pushButtonOK.setObjectName("pushButtonOK")
self.labelNumber = QtWidgets.QLabel(self.centralwidget)
self.labelNumber.setGeometry(QtCore.QRect(140, 190, 81, 41))
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(18)
self.labelNumber.setFont(font)
self.labelNumber.setObjectName("labelNumber")
self.labelNumber2 = QtWidgets.QLabel(self.centralwidget)
self.labelNumber2.setGeometry(QtCore.QRect(340, 190, 71, 41))
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 0, 0, 128))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.PlaceholderText, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 0, 0, 128))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.PlaceholderText, brush)
brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0, 128))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.PlaceholderText, brush)
self.labelNumber2.setPalette(palette)
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(28)
self.labelNumber2.setFont(font)
self.labelNumber2.setObjectName("labelNumber2")
self.labelName2 = QtWidgets.QLabel(self.centralwidget)
self.labelName2.setGeometry(QtCore.QRect(300, 100, 151, 71))
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(85, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 0, 0, 128))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.PlaceholderText, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(85, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 0, 0, 128))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.PlaceholderText, brush)
brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(240, 240, 240))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
brush = QtGui.QBrush(QtGui.QColor(0, 0, 0, 128))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.PlaceholderText, brush)
self.labelName2.setPalette(palette)
font = QtGui.QFont()
font.setFamily("隶书")
font.setPointSize(28)
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
self.labelName2.setFont(font)
self.labelName2.setObjectName("labelName2")
self.labelName = QtWidgets.QLabel(self.centralwidget)
self.labelName.setGeometry(QtCore.QRect(140, 110, 81, 51))
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(18)
self.labelName.setFont(font)
self.labelName.setMouseTracking(True)
self.labelName.setTextFormat(QtCore.Qt.AutoText)
self.labelName.setObjectName("labelName")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 544, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "学生抽点"))
self.pushButtonOK.setText(_translate("MainWindow", "确定(&E)"))
self.labelNumber.setText(_translate("MainWindow", "学号:"))
self.labelNumber2.setText(_translate("MainWindow", "0"))
self.labelName2.setText(_translate("MainWindow", "姓名"))
self.labelName.setText(_translate("MainWindow", "姓名:"))
3.窗体功能代码(Mywindows.py)
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from Windows import *
from choose import *
class MyWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setupUi(self)
self.pushButtonOK.pressed.connect(self.pushButtonOKpressed)
self.pushButtonOK.released.connect(self.pushButtonOKreleased)
def pushButtonOKpressed(self):
self.pushButtonOK.setCursor(QtCore.Qt.ClosedHandCursor)
def pushButtonOKreleased(self):
infor = ChooseStudent()
self.labelName2.setText(str(infor[1]))
self.labelNumber2.setText(str(infor[0]))
self.pushButtonOK.setCursor(QtCore.Qt.OpenHandCursor)
if __name__ == '__main__':
app = QApplication(sys.argv)
myWin = MyWindow()
myWin.show()
sys.exit(app.exec_())
4.项目发现
(1)发现一个pyqt designer的问题:
只能通过脚本 将设计好的.ui文件转换为.py文件,却不能逆向转化,不能实现代码和视图同步更新,使用时会有不方便的地方。因此要注意窗体设计和窗体功能的分离,将信号/槽都写入到窗体功能文件中,以免调整窗体设计时覆盖掉事件代码。
三、pyinstaller打包多个.py文件为.exe程序 1.具体操作 进入cmd
>> cd到当前项目的文件夹
>> 键入 pyinstaller -w -F Mywindows.py -p choose.py;Windows.py
2.补充说明
-F: 生成一个.exe文件
-w: 执行.exe文件时去除dos窗口
-p: 后跟需要调用的包文件,用分号分割
-i: 加入exe文件图标 例:pyinstaller -F -i XX.ico XX.py (还没尝试过)