# -*- codeing = utf-8 -*- # @Time:2021/3/20 17:29 # @Autor:杨宇佳 # @File: student.py # @Software : PyCharm
import xlwt import xlrd import os import shutil
def menu(): print("欢迎使用学生管理系统") print("*" * 50) print("1--添加学生信息") print("2--修改学生信息") print("3--删除学生信息") print("4-显示全部信息") print("5-根据名字查找学生信息") print("6-将信息导出到指定位置") print("0-退出系统") print("*" * 50)
def chance(): while True: inn = input("请用户输入0-6进入对应的功能") print(inn) if inn == '1': print("添加学生信息") add() elif inn == '2': print("修改学生信息") update() elif inn == '3': print("删除学生信息") dell() elif inn == '4': print("显示全部信息") insert() elif inn == '5': print("根据名字查找学生信息") name() elif inn == '6': print("将信息导出到指定位置") daoChu() elif inn == '0': print("退出系统") break
def add():
name = input("请输入学生姓名:") clazz = input("请输入的成绩科目:") score = input("请输入学生成绩:") stulist = [name, clazz, score]
rb = xlrd.open_workbook("student.xls") resheet = rb.sheets()[0] nrows = resheet.nrows ncols = resheet.ncols wb = xlwt.Workbook() sheet = wb.add_sheet("stu1") sheet.write(0, 0, "姓名") sheet.write(0, 1, "科目") sheet.write(0, 2, "成绩") s1 = xlwt.easyxf("pattern: pattern solid,fore_colour red") s2 = xlwt.easyxf("pattern: pattern solid,fore_colour white") for i in range(1, nrows): for j in range(ncols - 1): sheet.write(i, j, resheet.cell_value(i, j)) if int(resheet.cell_value(i, 2)) < 60: sheet.write(i, 2, resheet.cell_value(i, 2), s1) else: sheet.write(i, 2, resheet.cell_value(i, 2), s2)
sheet.write(nrows, 0, stulist[0]) sheet.write(nrows, 1, stulist[1]) if int(stulist[2]) < 60: sheet.write(nrows, 2, stulist[2], s1) else: sheet.write(nrows, 2, stulist[2], s2) wb.save("student.xls")
def update():
name = input("请输入要更改的学生姓名:") name1 = input("请输入更改后的学生姓名:") clazz = input("请输入更改后的成绩科目:") score = input("请输入更改后的学生成绩:") list = [name1, clazz, score]
rb = xlrd.open_workbook("student.xls") resheet = rb.sheets()[0] nrows = resheet.nrows ncols = resheet.ncols wb = xlwt.Workbook() sheet = wb.add_sheet("stu1") sheet.write(0, 0, "姓名") sheet.write(0, 1, "科目") sheet.write(0, 2, "成绩") s1 = xlwt.easyxf("pattern: pattern solid,fore_colour red") s2 = xlwt.easyxf("pattern: pattern solid,fore_colour white")
for i in range(1, nrows): if name == resheet.cell_value(i, 0): n = i continue else: for j in range(ncols - 1): sheet.write(i, j, resheet.cell_value(i, j))
if int(resheet.cell_value(i, 2)) < 60: sheet.write(i, 2, resheet.cell_value(i, 2), s1) else: sheet.write(i, 2, resheet.cell_value(i, 2), s2)
sheet.write(n, 0, list[0]) sheet.write(n, 1, list[1]) if int(list[2]) < 60: sheet.write(n, 2, list[2], s1) else: sheet.write(n, 2, list[2], s2) wb.save("student.xls")
def dell(): name = input("请输入要删除的学生姓名:") rb = xlrd.open_workbook("student.xls") resheet = rb.sheets()[0] nrows = resheet.nrows ncols = resheet.ncols wb = xlwt.Workbook() sheet = wb.add_sheet("stu1") sheet.write(0, 0, "姓名") sheet.write(0, 1, "科目") sheet.write(0, 2, "成绩") s1 = xlwt.easyxf("pattern: pattern solid,fore_colour red") s2 = xlwt.easyxf("pattern: pattern solid,fore_colour white") for i in range(1, nrows): if name == resheet.cell_value(i, 0): for k in range(i, nrows - 1): for j in range(ncols - 1): sheet.write(k, j, resheet.cell_value(k + 1, j)) break
else: for j in range(ncols - 1): sheet.write(i, j, resheet.cell_value(i, j))
if int(resheet.cell_value(i, 2)) < 60: sheet.write(i, 2, resheet.cell_value(i, 2), s1) else: sheet.write(i, 2, resheet.cell_value(i, 2), s2) wb.save("student.xls")
def insert():
wb = xlrd.open_workbook('student.xls') sheet = wb.sheets()[0] nrows = sheet.nrows ncols = sheet.ncols for i in range(nrows): for j in range(ncols): cell = sheet.cell_value(i, j) print(cell, end='\t') print()
def name():
name = input("请输入要查找的学生姓名:") rb = xlrd.open_workbook("student.xls") resheet = rb.sheets()[0] nrows = resheet.nrows ncols = resheet.ncols for i in range(nrows): if name == resheet.cell_value(i, 0): for j in range(ncols): cell = resheet.cell_value(i, j) print(cell, end="\t\t") print()
def daoChu():
newpath = input("请输入导出文件的目标路径:") oldpath="student.xls" f = os.path.exists(oldpath) if f == True: os.makedirs(newpath) else: print("需要导出文件的路径不存在,请重新输入:") shutil.copy(oldpath, newpath)
if __name__=="__main__": menu() chance()
|
# -*- codeing = utf-8 -*- # @Time:2021/3/22 15:26 # @Autor:杨宇佳 # @File: student.py # @Software : PyCharm # -*- codeing = utf-8 -*- # @Time:2021/3/22 11:10 # @Autor:杨宇佳 # @File: Demo02.py # @Software : PyCharm
def menu(): print("欢迎使用学生管理系统") print("*" * 50) print("1--添加学生信息") print("2--修改学生信息") print("3--删除学生信息") print("4-显示全部信息") print("5-根据名字查找学生信息") print("0-退出系统") print("*" * 50)
def chance(): while True: inn = input("请用户输入0-6进入对应的功能") print(inn) if inn == '1': print("添加学生信息") add() elif inn == '2': print("修改学生信息") update() elif inn == '3': print("删除学生信息") dell() elif inn == '4': print("显示全部信息") insert() elif inn == '5': print("根据名字查找学生信息") name() elif inn == '0': print("退出系统") break
import pymysql def add(): conn=pymysql.connect(host="localhost",user="root",password='591670',database='student') cursor=conn.cursor() sql='insert into students(sid,uname,age) values (%s,%s,%s)' sid=input("输入编号") uname=input("输入名字") age=input("输入年龄") datas=[(sid,uname,age)] num=cursor.executemany(sql,datas) cursor.close() conn.commit() conn.close()
def update(): conn=pymysql.connect(host="localhost",user="root",password="591670",database='student') cursor=conn.cursor() sid=input("输入要更改的学号") sql='select * from students where sid=%s' cursor.execute(sql,sid) rs=cursor.fetchone() if rs: sql='update students set uname=%s,age=%s where sid=%s' uname = input("输入名字") age = input("输入年龄") datas = [(uname, age,sid)] num = cursor.executemany(sql, datas) cursor.close() conn.commit() conn.close() print("修改成功") else: print("无此学生")
def dell(): conn=pymysql.connect(host="localhost",user="root",password="591670",database='student') cursor=conn.cursor() sid = input("输入要删除的学号") sql='delete from students where sid=%s' cursor.execute(sql,sid) cursor.close() conn.commit() conn.close()
def insert(): conn=pymysql.connect(host="localhost",user="root",password="591670",database='student') cursor=conn.cursor() sql='select * from students' cursor.execute(sql) while True: rs=cursor.fetchone() if rs is None: break print(rs) conn.close()
def name(): conn=pymysql.connect(host="localhost",user="root",password="591670",database='student') cursor=conn.cursor() sql='select * from students where uname=%s' uname=input("输入要查找的学生名字") cursor.execute(sql,uname) rs=cursor.fetchone() print(rs) conn.close()
if __name__=="__main__": menu() chance()
|