一、介绍
主要功能实现
1、借书
2、添加新书
3、查找图书
数据存储:excel表
界面:cmd窗口
import xlwt
import xlrd
import xlutils.copy
total_books = 0
#book = {"书名":"","作者":"","价格":"","编号":"","位置":""}
def read_book_num():
book_excel = xlrd.open_workbook("图书.xls")
sheet1 = book_excel.sheets()[0]
book_num = sheet1.nrows
return book_num
def new_book():
global total_books
book = []
print_value = ("书名","作者","价格","编号","位置")
input_value = ""
#打开存储 总书数量为0直接覆盖添加,否则打开
if(total_books == 0):
book_excel = xlwt.Workbook("图书.xls")
sheet1 = book_excel.add_sheet("1")
else:
book_excel1 = xlrd.open_workbook("图书.xls")
book_excel = xlutils.copy.copy(book_excel1)
sheet1 = book_excel.get_sheet(0)
while(1):
#打印提示
print("添加新书:")
book = []#清空书本信息
input_value = ""#清空输入
#输入
for i in range(0,5):
print("请输入:"+ print_value[i])
input_value = input()
#判断是否退出
if(input_value == 'q'):
return
book.append(input_value)
#存储到硬盘
for i in range(0,5):
sheet1.write(total_books, i, book[i])
total_books += 1 #总书数量加1
book_excel.save("图书.xls")
print("添加成功")
return
def search():
#打开excel
book_excel = xlrd.open_workbook("图书.xls")
sheet1 = book_excel.sheets()[0]
book_num = sheet1.nrows
while(1):
#输入书名
bookname = input("请输入书名:")
if(bookname == 'q'):
return
#查找
for i in range(0, book_num):
if( bookname == sheet1.cell(i, 0).value ):
#返回结果
print(sheet1.row_values(i))
return
def borrow():
#打开excel
book_excel = xlrd.open_workbook("图书.xls")
sheet1 = book_excel.sheets()[0]
book_num = sheet1.nrows
book_excel_w = xlwt.Workbook("图书.xls")
sheet_w = book_excel_w.add_sheet()
while(1):
#输入书名
bookname = input("请输入借书书名:\n按q退出借书界面")
if(bookname == 'q'):
return
#查找
for i in range(0, book_num):
if( bookname == sheet1.cell(i, 0).value ):
sheet_w.write(i, 6, '')#清除位置
return
def main_window():
global total_books
total_books = read_book_num()#获取系统里面有多少本书
while(1):
print("图书管理系统")
print("1.新书加入")
print("2.查询")
print("3.借书")
x = int(input("请输入你的操作"))
if(x == 1):
new_book()
elif(x == 2):
search()
elif(x == 3):
borrow()
else:
print("输入无效")
return
main_window()