'''
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")
'''
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")
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")