图书管理系统第一次复习

列表增删改查

test01.py

'''
import xlrd
book = xlrd.open_workbook("e://person.xls")
#sheet1 = book.sheet_by_index(0)
sheet1 = book.sheet_by_name("person")
#a = sheet1.cell(2,3).value
#一共多少行
r = sheet1.nrows
#一共多少列
c = sheet1.ncols
for i in range(1,r):
    for j in range(c):
        print(sheet1.cell(i,j).value,end = " ")
    print()




import xlrd,xlwt
from xlutils import copy

book = xlrd.open_workbook("e://person.xls")
book_copy = copy.copy(book)

sheet = book_copy.get_sheet(0)
sheet.write(2,1,"霍锦惜")
book_copy.save("e://person.xls")


import xlrd,xlwt
from xlutils import copy

book = xlrd.open_workbook("e://person.xls")

r = book.sheet_by_name("person").nrows
c = book.sheet_by_name("person").ncols

book_copy = copy.copy(book)
sheet = book_copy.get_sheet(0)

a = ["3","张三","18","147","男","0"]
for i in range(c):
    sheet.write(r,i,a[i])

book_copy.save("e://person.xls")
'''

test02.py

'''
username = input("请输入用户名:")
password = input("请输入密码:")
f = test20190310.login(username,password)
print(f)

#test20190310.showBooks()
a = ["1","2"]
test20190310.addBook(a)

test20190310.deleteBook(1)
test20190310.showBooks()
'''
#import hashlib
#print(hashlib.md5("root".encode("utf-8")).hexdigest())

#import hashlib
#print(hashlib.md5("admin".encode("utf-8")).hexdigest)

#print(test20190310.login("admin","admin"))

图书管理系统( ## 建两个.py文件)

test1.py

import xlrd
from xlutils.copy import copy
import hashlib
def login(username,password):
    #读取excel信息
    book = xlrd.open_workbook("book.xls")
    sheet = book.sheet_by_name("user")
    r = sheet.nrows
    c = sheet.ncols
    flag = False
    '''
    m---username  列下标
    p---password
    q---state
    '''
    m = 0
    p = 0
    q = 0
    for j in range(c):
        if sheet.cell(0, j).value == "username":
            m = j
        elif sheet.cell(0, j).value == "password":
            p = j
        elif 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 = 0
    for j in range(c):
        if sheet.cell(0,j).value == "state":
            q = j
    for i in range(1, r):
        if sheet.cell(i,q).value == "1":
            continue
        for j in range(c):
            if j == q:
                continue
            print(sheet.cell(i,j).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(r,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).value == a[0]:
            cr = i
            break
    for i in range(c):
        try:
            sheet_copy.write(cr,i,a[i])
        except:
            break
    book_copy.save("book.xls")
def deleteBook(id):
    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")




test2.py

import test20190310 as book
import hashlib
import xlrd
from xlutils import copy


print("=============欢迎登录图书管理系统================")
username = input("请输入用户名:")
password = input("请输入密码:")
count = 0
while count < 3:
    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
        elif op == "3":
            a = input("请输入图书编号:")
            b = input("请输入图书名字:")
            c = input("请输入图书出版社:")
            d = input("请输入图书价格:")
            e = input("请输入图书作者:")
            book_ = [a, b, c, d, e]
            book.updateBook(book_)
            break
        elif op == "4":
            id = input("请输入图书编号:")
            book.deleteBook(id)
            break
        else:
            print("请输入正确编号!")
            break
    else:
        print("用户名或密码错误")
        count += 1
if count == 3:
    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(r,1).value == username:
            sheet_copy.write(r,3,"1")
            break
    book_copy.save("book.xls")







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值