#先定义各项函数功能##test0118
import hashlib
import xlrd
import xlwt
from xlutils.copy import copy
# 登录功能
def login(username, password):
# 获取用户表格
book = xlrd.open_workbook("book.xls")
sheet = book.sheet_by_name("user")
# 获取行,列
r = sheet.nrows
c = sheet.ncols
m = 0
p = 0
q = 0
flag = False
for j in range(c):
'''m,username所在的列下标;
p,password的列下标;
q,state的列下标'''
if sheet.cell(0, j).value == "username":
m = j
if sheet.cell(0, j).value == "password":
p = j
if sheet.cell(0, j).value == "state":
q = j
password = hashlib.md5(password.encode("utf-8")).hexdigest()
# 查表,对比用户名和密码
for i in range(1, r):
if sheet.cell(i, q).value == "0" and sheet.cell(i, m).value == username and sheet.cell(i, p).value == password:
flag = True
break
return flag
# 显示书籍信息
def showBooks():
book = xlrd.open_workbook("book.xls")
sheet = book.sheet_by_name("book")
r = sheet.nrows
c = sheet.ncols
# q,state所在的列下标,
q = 0
for j in range(c):
# 获取book的state所在列的下标,q
if sheet.cell(0, j).value == "state":
q = j
for i in range(1, r):
if sheet.cell(i, q).value == "1":
continue # 状态为1 ,直接跳出本次循环
for k in range(c):
if k == q: # state列的信息不对用户显示
continue
print(sheet.cell(i, k).value, end="") # 显示图书信息
print()
def addBook(a): # 添加图书信息
book = xlrd.open_workbook("book.xls")
sheet = book.sheet_by_name("book")
r = sheet.nrows
c = sheet.ncols
book_copy = copy(book)
sheet_copy = book_copy.get_sheet(1)
for i in range(c):
#将输入的数据存入工作表
try: # 可能发生异常的代码
sheet_copy.write(a, i, a[i]) # a[i]是输入的图书信息,是个列表
except:
break
book_copy.save("book.xls")
def updateBook(a): # 修改/更新书籍信息
book = xlrd.open_workbook("book.xls")
sheet = book.sheet_by_name("book")
r = sheet.nrows
c = sheet.ncols
book_copy = copy(book)
sheet_copy = book_copy.get_sheet(1)
cr = 0 # 标记要修改的行号
for i in range(r):
# 接收要修改的行号
if sheet.cell(i, 0) == a[0]:
cr = i
break
for i in range(c):
try:
sheet_copy.write(r, i, a[i])
except:
break
book_copy.save("book.xls")
def deleteBook(a): # 删除图书
book = xlrd.open_workbook("book.xls")
sheet = book.sheet_by_name("book")
r = sheet.nrows
c = sheet.ncols
book_copy = copy(book)
sheet_copy = book_copy.get_sheet(1)
cr = 0
for i in range(r):
if sheet.cell(i, 0).value == "id":
cr = i
break
sheet_copy.write(cr, c - 1, "1")
book_copy.save("book.xls")
#合理调用各个函数
import test0118 as book
import hashlib
import xlrd
import xlwt
from xlutils import copy
print("==========欢迎登陆图书管理系统=========")
count = 0#登陆次数
while count<3:
username = input("请输入用户名:")
password = input("请输入密码:")
result = book.login(username,password)
if result:
print("1---查看图书信息")
print("2---添加图书信息")
print("3---修改图书信息")
print("4---删除图书信息")
op = input("请输入操作编号:")
if op == "1":
book.showBooks()
break
elif op =="2":
a =input("请输入图书编号")
b = input("请输入图书名称")
c = input("请输入图书出版社")
d = input("请输入图书价格")
e = input("请输入图书作者")
book_=[a,b,c,d,e,"0"]
book.addBook(book_)
break
#print("========%s========""end")
elif op == "3":
a = input("请输入图书编号:")
b = input("请输入图书名字:")
c = input("请输入图书出版社:")
d = input("请输入图书价格:")
e = input("请输入图书作者:")
#book.updateBook(book_)
break
elif op =="4":
id = input("请输入图书编号")
book.deleteBook(id)
break
else:
print("请输入正确编号")
break
pass
else:
print("用户名或密码错误")
count +=1
if count == 3:
print("账号已锁定")
book = xlrd.open_workbook("book.xls")
sheet = book.sheet_by_name("user")
# 获取行,列
r = sheet.nrows
c = sheet.ncols
book_copy = copy.copy(book)
sheet_copy = book_copy.get_sheet(0)
for i in range(r):
if sheet.cell(i,1).value == username:
sheet_copy.write(i,3,"1")
break
book_copy.save("book.xls")
#
1 要在当前存在的excel写入信息,导入from xlutils.copy import copy
2、使用xlrd.open_workbook 打开excel文件,
3、使用copy 方法创建一个副本,也就是我们用xlwt的xlwt.Workbook() 创建一个Wbook对象
4、使用get_sheet() 方法来获得要写入数据的页,
5、使用write方法来写入数据write(row, col, value, style)
import test0118
import hashlib
#md5加密
#print(hashlib.md5("root".encode("utf-8")).hexdigest())
'''
函数的参数
1.必须参数 required argument(实参); parameter(形参)
2.默认参数 a=9
3.关键字参数 asf(age="18",name="nana")
4.不定长参数 *a ;**a 字典
'''
'''def asd(b,a=9):#先写必须参数,再写默认参数
print(b,a)
asd(15,5)
def asf(name,age):
print(name,age)
asf(age="18",name="nana")
def show_info(*a):
print(a)
show_info("张三丰",180,"男")
'''