xlrd、xlwt、xlutils、sys、hashlib
def main():
while True:
print("\n\n **********************")
print(" *欢迎来到图书管理系统* ")
print(" **********************\n")
print("********************************")
print("****** 登录------1 ***********")
print("****** 注册------2 ***********")
print("****** 退出------0 ***********")
print("********************************\n")
x = int(input("请输入对应数字: "))
if x==2:
def login(username,password):
users = xlrd.open_workbook("users.xls")
sheet = users.sheet_by_name("users")
r = sheet.nrows
c = sheet.ncols
flag = False
m = 0 #用户名所在的列下标
p = 0 #密码所在的列下标
q = 0 #状态所在的列下标
for i in range(c):
if sheet.cell(0,i).value == "username":
m=i
elif sheet.cell(0,i).value == "password":
p=i
elif sheet.cell(0,i).value == "state":
q=i
for j in range(1,r):
if sheet.cell(j,q).value == "0" and sheet.cell(j,m).value == md5(username) and sheet.cell(j,p).value == md5(password):
flag = True
break
elif sheet.cell(j,q).value == "1" and sheet.cell(j,m).value == md5(username) and sheet.cell(j,p).value == md5(password):
flag = False
print("该账户已锁定。")
break
return flag
def register(username,password):
# md5加密
hashname = hashlib.md5()
hashname.update(bytes(username,encoding="utf-8"))
hashpass = hashlib.md5()
hashpass.update(bytes(password, encoding="utf-8"))
users = xlrd.open_workbook("users.xls")
sheet = users.sheet_by_name("users")
users_copy = copy.copy(users)
sheet_copy = users_copy.get_sheet(0)
r = users.sheet_by_index(0).nrows
c = users.sheet_by_index(0).ncols
sheet_copy.write(r,0,r)
sheet_copy.write(r,1,hashname.hexdigest())
sheet_copy.write(r,2,hashpass.hexdigest())
sheet_copy.write(r,3,"0")
users_copy.save("users.xls")
print("注册成功!")
我通过sys包里的exit(0)来实现的
elif x==0:
sys.exit(0)
def fun():
while True:
print("\n**************************************")
print("******** 添加图书--------1 ********")
print("******** 删除图书--------2 ********")
print("******** 查找图书--------3 ********")
print("******** 修改图书--------4 ********")
print("******** 查看所有图书----5 ********")
print("******** 返回主界面------6 ********")
print("******** 退出------------0 ********")
print("**************************************\n")
v = int(input("请输入对应的数字: "))
def tianjia():
book = xlrd.open_workbook("book.xls")
book_copy = copy.copy(book)
sheet_copy = book_copy.get_sheet(0)
id = input("请输入id:")
name = input("请输入名字: ")
publish = input("请输入出版社: ")
author = input("请输入作者: ")
price = input("请输入价格: ")
state = input("请输入状态(0/1): ")
a=[id,name,publish,author,price,state]
r = book.sheet_by_index(0).nrows
c = book.sheet_by_index(0).ncols
for i in range (c):
sheet_copy.write(r,i,a[i])
print("添加完毕!")
book_copy.save("book.xls")
print("保存成功!")
def shanchu():
book = xlrd.open_workbook("book.xls")
sheet = book.sheet_by_name("person")
book_copy = copy.copy(book)
sheet_copy = book_copy.get_sheet(0)
name = input("请输入你要删除的图书名: ")
r = book.sheet_by_index(0).nrows
c = book.sheet_by_index(0).ncols
m = 0 # 图书名所在的列下标
for i in range(r):
if sheet.cell(i,1).value == name:
m = i
for j in range(c):
sheet_copy.write(m,j," ")
book_copy.save("book.xls")
print("删除书籍成功!")
def chazhao():
book = xlrd.open_workbook("book.xls")
sheet = book.sheet_by_name("person")
print("可以输入查找图书id和书名任意一个.")
id = input("请输入图书id: ")
name = input("请输入图书名: ")
# 行数
r = sheet.nrows
# 列数
c = sheet.ncols
m = 0 #id所在的列下标
p = 0 #书名所在的列下标
for i in range(r):
if sheet.cell(i,0).value == id or sheet.cell(i,1) == name:
m = i
p = i
for j in range(1,c):
print(sheet.cell(m,j).value,end = " ")
print()
def xiugai():
#打开工作簿
book = xlrd.open_workbook("book.xls")
#创建副本
book_copy = copy.copy(book)
#打开工作表
sheet_copy = book_copy.get_sheet(0)
#修改目标位置
r,c=eval(input("请输入要修改的坐标位子行,列: "))
change = input("请输入修改内容: ")
sheet_copy.write(r,c,change)
print("修改完毕!")
#保存
book_copy.save("book.xls")
print("保存成功!")
def chakan():
book = xlrd.open_workbook("book.xls")
sheet = book.sheet_by_name("person")
#sheet = book.sheet_by_index(0)
#行数
r=sheet.nrows
#列数
c=sheet.ncols
for i in range (1,r):
for j in range (0,c):
print(sheet.cell(i,j).value,end=" ")
print()
完整代码请到这里下载———>SystemBooks代码
上一篇文章———>微信红包程序,发吉利数字红包
下一篇文章———>Python 给下载文件显示进度条和下载时间