图书管理系统--python

一、介绍
主要功能实现
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()


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值