Python图书管理系统程序设计(SystemBooks)

实现界面部分功能展示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

一)、图书管理系统

(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 给下载文件显示进度条和下载时间

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章