圖書管理系統
實現界面部分功能展示
一)、圖書管理系統
(1)、所使用模塊與包有:
xlrd、xlwt、xlutils、sys、hashlib
(2)、完成步驟(展示部分代碼,需要全部代碼請看文章最底下鏈接):
1、建立入口界面主函數Mian
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:
2、建立登陸函數模塊(內含md5加密功能)
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
3、建立註冊函數模塊(內含md5加密功能)
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("註冊成功!")
4、建立退出函數模塊
我通過sys包裏的exit(0)來實現的
elif x==0:
sys.exit(0)
5、建立圖書管理系統界面次函數fun()
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("請輸入對應的數字: "))
6、建立添加圖書函數模塊
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("保存成功!")
7、建立刪除函數模塊
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("刪除書籍成功!")
8、建立查找圖書模塊
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()
9、建立修改圖書模塊
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("保存成功!")
10、建立查看所有圖書模塊
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 給下載文件顯示進度條和下載時間