学习目标:
提示:编写图书管理系统
学习内容:
实现增删改查的简单操作以及学生的借阅归还操作,并简单掌握python对excel的操作
学习时间:
三天内编写 2022年06月30日
学习产出:
- 加深对python语法的理解
- 了解python拓展包的安装
- 了解openpyxl的使用
安装openpyxl拓展包:
1.更新pip
win+r输入cmd+回车打开终端
输入
python -m pip install --upgrade pip
2.安装openpyxl
pip install openpyxl
代码分享:
from openpyxl import Workbook, load_workbook
import os
import time
import sys
Length = 1
stu_num = 0
operations = []
t = 0
# 创建xlsx表来储存内容
def CreateList():
global Length
Length = 1
wb = Workbook()
wb.create_sheet('图书信息')
wb.create_sheet('学生信息')
ws = wb['图书信息']
ws['A1'].value = '书名'
ws['B1'].value = '作者'
ws['C1'].value = '数量'
ws = wb['学生信息']
wb.save('library.xlsx')
# 初始化长度
def ini_Length():
global Length
Length += 1
wb = load_workbook('library.xlsx')
ws = wb['图书信息']
while ws['A'+str(Length)].value != None:
Length += 1
Length -= 1
def ini_stu():
global stu_num
stu_num += 1
wb = load_workbook('library.xlsx')
ws = wb['学生信息']
while ws['A'+str(stu_num)].value != None:
stu_num += 1
stu_num -= 1
# 阻止无效输入
def int_input(operate):
if operate == 'append':
try:
num = int(input("您的下一步操作是(1.继续 2.返回):"))
if num == 1 or num == 2:
return num
else:
1/0
except:
print("此输入无效!请重新输入-")
print()
return int_input(operate)
elif operate == 'delete':
try:
detail()
num = int(input('请输入你将要删除图书的序号(-1 :返回):'))
if 0 < num < Length:
return num+1
else:
1/0
except:
print("此输入无效!请重新输入-")
print()
return int_input(operate)
elif operate == 'borrow':
try:
num = int(input('请输入你将要借阅图书的序号(-1 :返回):'))
if num == -1:
return -1
elif 0 < num < Length:
return num+1
else:
1/0
except:
print("此输入无效!请重新输入-")
print()
return int_input(operate)
elif operate == 'find':
try:
num = int(input('\n1.总览\n2.按名称查询\n3.按作者查询\n\n请输入你的操作:'))
if 0 < num < 4:
return num
else:
1/0
except:
print("此输入无效!请重新输入-")
print()
return int_input(operate)
elif operate == 'amount':
try:
num = int(input("请输入图书的数量:"))
if num < 0:
1/0
else:
return num
except:
print("此输入无效!请重新输入-")
print()
return int_input(operate)
elif operate == 'admin':
try:
num = int(input("您的下一步操作是:"))
if 1 <= num <= 6 or num == -1:
return num
else:
1/0
except:
print("此输入无效!请重新输入-")
print()
return int_input(operate)
elif operate == 'main':
try:
num = int(input("输入数字可进行相应操作:"))
if num == 1 or num == -1 or num == 2:
return num
else:
1/0
except:
print("此输入无效!请重新输入-")
print()
return int_input(operate)
elif operate == 'ini':
try:
num = int(input("您的操作是:"))
if num == 1 or num == 2:
return num
else:
1/0
except:
print("此输入无效!请重新输入-")
print()
return int_input(operate)
elif operate == 'stu_num':
try:
users = int(input('请输入学号(10位):'))
if len(str(users)) != 10:
1/0
else:
return users
except:
print("此输入无效!请重新输入!!!")
return int_input(operate)
elif operate == 'student':
try:
num = int(input('请输入您的操作:'))
if 4 > num > 0:
return num
else:
1/0
except:
print("此输入无效!请重新输入!!!")
return int_input(operate)
elif operate == 'return_book':
try:
num = int(input('\n\n请输入您需要归还图书对应的序号(-1 :返回):'))
if t >= num > 0 or num == -1:
return num
else:
1/0
except:
print("此输入无效!请重新输入!!!")
return int_input(operate)
elif operate == 'revise_book':
detail()
try:
num = int(input('\n\n请输入您需要修改图书数量对应的序号(-1 :返回):'))
if 0 < num < Length or num == -1:
return num
else:
1/0
except:
print("此输入无效!请重新输入!!!")
return int_input(operate)
else:
wb = load_workbook('library.xlsx')
ws = wb['图书信息']
amount = ws['C'+str(operate)].value
try:
num = int(input('\n\n请输入需要修改图书数量(-1 :返回):'))
if abs(num) <= amount or num > amount:
ws['C'+str(operate)].value += num
wb.save('library.xlsx')
else:
1/0
except:
print("此输入无效!请重新输入!!!")
return int_input(operate)
# 增加图书
def append_Book():
global Length
judge = 0
wb = load_workbook('library.xlsx')
ws = wb['图书信息']
name = input("请输入书名:")
author = input("请输入作者:")
num = int_input('amount')
for i in range(2, Length+1):
if ws['A'+str(i)].value == name:
ws['C'+str(i)].value += num
wb.save('library.xlsx')
judge = 1
if judge == 1:
print('此书已录入,已自动帮您添加该书的数量!!')
time.sleep(3)
admin_operate()
else:
ws.append([name, author, num])
wb.save('library.xlsx')
print('Lenght = ', Length)
Length += 1
judge = int_input('append')
if judge == 1:
append_Book()
else:
admin_operate()
# 删除图书
def delete_Book(num):
global Length
if Length == 0:
print("您还没有存入图书!")
return 0
wb = load_workbook('library.xlsx')
ws = wb['图书信息']
ws.delete_rows(num)
wb.save('library.xlsx')
Length -= 1
# 修改图书
def revise_Book():
print("此功能只支持修改书本的数量!!")
print()
num = int_input('revise_book')
if num == -1:
pass
else:
int_input(num)
# 总览
def detail():
alph = ['A', 'B', 'C']
wb = load_workbook('library.xlsx')
ws = wb['图书信息']
for i in range(1, Length+1):
print('-------------------')
for j in alph:
print(ws[j+str(i)].value, end='\t')
if i != 1:
print(" .........\t", i-1)
print()
# 查询
def find():
judge = 0
alph = ['A', 'B', 'C']
operate = int_input('find')
if operate == 1:
detail()
elif operate == 2:
name = input("\n请输入您查询图书的名称:")
wb = load_workbook('library.xlsx')
ws = wb['图书信息']
for i in range(2, Length+1):
if name == ws['A'+str(i)].value:
for j in alph:
print(ws[j+str(1)].value, end='\t')
print()
for j in alph:
print(ws[j+str(i)].value, end='\t')
judge = 1
print()
break
if judge == 0:
print("此系统未录入此书!")
time.sleep(2)
return 0
elif operate == 3:
name = input('\n请输入您查询作者的姓名:')
wb = load_workbook('library.xlsx')
ws = wb['图书信息']
for i in range(2, Length+1):
if name == ws['B'+str(i)].value:
for j in alph:
print(ws[j+str(1)].value, end='\t')
print()
for j in alph:
print(ws[j+str(i)].value, end='\t')
judge = 1
print()
if judge == 0:
print("此系统未录入该作者的书籍!")
time.sleep(2)
return 0
# 初始化图书
def ini_library():
global operations
global Length
print("*********************************")
print('* ', end='')
for i in operations:
print('{} -->'.format(i[0]), end='')
print(" \t*")
print("* *")
print("*您将丢失所有包括图书及学生在内\t*")
print("*的信息!!! \t*")
print("* \t*")
print("* 1.确定 \t*")
print("* 2.我再想想 \t*")
print("* *")
print("* \t*")
print("*********************************")
print()
operate = int_input('ini')
if operate == 1:
print("格式化中请稍等", end=' ')
time.sleep(2)
print('.', end=' ')
time.sleep(2)
print(". 数据已成功清除!")
time.sleep(2)
Length = 0
CreateList()
operations.pop()
Length += 1
admin_operate()
# 借阅图书
def borrow_book(user):
global Length
global stu_num
num = find()
if num != 0:
num = int_input('borrow')
else:
return 0
if num == -1:
return 0
if Length == 0:
print("当前图书馆还没有录入图书!")
return 0
wb = load_workbook('library.xlsx')
ws = wb['图书信息']
book_name = ''
if ws['C'+str(num)].value <= 0:
print("该图书没有库存了!")
else:
ws['C'+str(num)].value -= 1
book_name = ws['A'+str(num)].value
ws = wb['学生信息']
for i in range(1, stu_num+1):
if ws['A'+str(i)].value == user:
if ws['D'+str(i)].value != None:
print()
print("您已经借阅了三本图书请归还后再借阅,谢谢您的配合!")
time.sleep(2)
ws = wb['图书信息']
ws['C'+str(num)].value += 1
else:
if ws['B'+str(i)].value == None:
ws['B'+str(i)].value = book_name
elif ws['C'+str(i)].value == None:
ws['C'+str(i)].value = book_name
else:
ws['D'+str(i)].value = book_name
break
wb.save('library.xlsx')
# 归还图书操作
def return_book(user):
global t
wb = load_workbook('library.xlsx')
ws = wb['学生信息']
name = ''
for i in range(1, stu_num+1):
if ws['A'+str(i)].value == user:
if ws['B'+str(i)].value != None:
t += 1
print(t, '.', ws['B'+str(i)].value, '\t', end='')
if ws['C'+str(i)].value != None:
t += 1
print(t, '.', ws['C'+str(i)].value, '\t', end='')
if ws['D'+str(i)].value != None:
t += 1
print(t, '.', ws['D'+str(i)].value)
c = i
break
if t == 0:
print("本图书馆没有您的借阅记录!")
time.sleep(2)
return 0
num = int_input('return_book')
if num == -1:
pass
elif num == 1:
name = ws['B'+str(c)].value
ws['B'+str(c)].value = ws['C'+str(c)].value
ws['C'+str(c)].value = ws['D'+str(c)].value
ws['D'+str(c)].value = None
elif num == 2:
name = ws['C'+str(c)].value
ws['C'+str(c)].value = ws['D'+str(c)].value
ws['D'+str(c)].value = None
elif num == 3:
name = ws['D'+str(c)].value
ws['D'+str(c)].value = None
ws = wb['图书信息']
for i in range(1, Length):
if ws['A'+str(i)].value == name:
ws['C'+str(i)].value += 1
wb.save('library.xlsx')
t = 0
# 管理员操作菜单
def admin_operate():
global Length
global stu_num
global operations
print()
print('*********************************')
print('* ', end='')
for i in operations:
print('{}->'.format(i[0]), end='')
print(' \t*')
print('* \t*')
print('* 1.增加图书 \t*')
print('* 2.删除图书 \t*')
print('* 3.修改数值 \t*')
print('* 4.查询图书 \t*')
print('* 5. 初始化 \t*')
print('* 6. 返回 \t*')
print('* \t*')
print('* -1.保存并退出 \t*')
print('* \t*')
print('*当前录入图书数量:{} \t*'.format(Length-1))
print('*学生用户数量:{} \t*'.format(stu_num))
print('*********************************')
print()
# 学生端菜单
def student_operate():
print()
print('*********************************')
print('* ', end='')
for i in operations:
print('{}->'.format(i[0]), end='')
print(' \t*')
print('* \t*')
print('* 1.借阅图书 \t*')
print('* 2.归还图书 \t*')
print('* 3. 返回 \t*')
print('* \t*')
print('* \t*')
print('*********************************')
# 管理入口
def administrator():
global operations
global Length
admin_operate()
operation = int(input('请输入你的操作:'))
judge = 1
while judge > 0:
if operation == 1:
append_Book()
elif operation == 2:
name = int_input('delete')
if name != -1:
delete_Book(name)
admin_operate()
elif operation == 3:
revise_Book()
elif operation == 4:
find()
print("按任意键返回~ ~ ~")
os.system('pause')
print()
admin_operate()
elif operation == 5:
operations.append(['5.初始化', 'administrator'])
ini_library()
print()
print("感谢您的使用!请重新进入")
time.sleep(5)
sys.exit(0)
elif operation == 6:
t = operations.pop()
main()
elif operation == -1:
print("感谢您的使用!")
time.sleep(5)
sys.exit(0)
operation = int_input('admin')
# 学生操作入口
def student():
global operations
global stu_num
user = int_input('stu_num')
judge = 1
wb = load_workbook('library.xlsx')
ws = wb['学生信息']
for i in range(1, stu_num+1):
if ws['A'+str(i)].value == user:
judge = 0
break
if judge:
stu_num += 1
ws['A'+str(stu_num)].value = user
wb.save('library.xlsx')
student_operate()
operate = int_input('student')
while 1:
if operate == 1:
borrow_book(user)
student_operate()
elif operate == 2:
return_book(user)
student_operate()
elif operate == 3:
operations.pop()
main()
operate = int_input('student')
# 主函数入口
def main():
global Length
if os.path.exists(r'library.xlsx'):
pass
else:
CreateList()
global operations
print('*********************************')
print('* \t*')
print('* 1.管理员 \t*')
print('* 2.学生 \t*')
print('* \t*')
print('* -1.退出 \t*')
print('* \t*')
print('*********************************')
print()
ini_Length()
ini_stu()
operate = int_input('main')
if operate == 1:
operations.append(['1.管理员', 'main'])
administrator()
elif operate == 2:
operations.append(['2.学生', 'main'])
student()
elif operate == -1:
print("感谢您的使用!")
time.sleep(5)
sys.exit(0)
main()