亲宝软件园·资讯

展开

python图书管理系统 基于python实现图书管理系统

昇柱 人气:0
想了解基于python实现图书管理系统的相关内容吗,昇柱在本文为您仔细讲解python图书管理系统的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:python,图书管理系统,下面大家一起来学习吧。

添加新书

查询

借阅

二次添加新书(读取已有的.xls并修改)

代码:

import xlwt
import xlrd


def read_old_data(row0_len):
    try:
        filename=".\图书.xls"
        old_data = []#读取表格已有内容
        data = xlrd.open_workbook(filename)
        sheet0 = data.sheet_by_index(0)
        nrows = sheet0.nrows  #获取该sheet中的有效行数
        print("Info:读取到已有数据表格")
        print("有效行数:",nrows)
        for i in range(nrows):
            for j in range(row0_len):
                old_data.append(sheet0.cell(i,j).value)
        print("共有旧的数据:",len(old_data))
    except IOError:
        print("Info: 没有找到文件或读取文件失败/n1 =>新建图书.xls文件")
        nrows=0
    return old_data,nrows

def new_book():

    book = []
    print_value=("书名","作者","编号","位置","数量")
    row0_len=len(print_value)#列数
    input_value=''
    '''
    try:

    '''
    (old_data,nrows)=read_old_data(row0_len)
    
    #打开存储
    book_excel = xlwt.Workbook()
    sheet1 = book_excel.add_sheet("books",cell_overwrite_ok=0)

    #写入旧数据:
    for i in range(nrows):
        for j in range(row0_len):
            sheet1.write(i,j,old_data[(i*row0_len)+j])

    while(1):
        print("添加新书")
        #输入
        for i in range(row0_len):
            print("请输入:"+print_value[i])
            input_value = input()

            #判断是否输出
            if(input_value == 'q'):
                book_excel.save("图书.xls")
                return
            book.append(input_value)
        #保存到硬盘
        for i in range(row0_len):
            sheet1.write(nrows,i,book[i])
        nrows=nrows+1
        book=[]#清空book缓存
    return


def search():
    #打开excel
    book_excel = xlrd.open_workbook("图书.xls")
    sheet1 = book_excel.sheets()[0]
    book_num =sheet1.nrows
    #while(1):
    #输入书名
    bookname = input("请输入书名:")
    find_flag=0
    #查找
    for i in range(book_num):
        if(bookname == sheet1.cell_value(i,0)):
            if(int(sheet1.cell_value(i,4))>0):
                find_flag=1
                print("书名:",sheet1.cell_value(i,0))
                print("作者:",sheet1.cell_value(i,1))
                print("位置:",sheet1.cell_value(i,3))
                print("库存(本):",sheet1.cell_value(i,4))
                return
    if(find_flag==1):
        print("查无此书。")
    return

def borrow():
    #打开excel
    book_excel =xlrd.open_workbook("图书.xls")
    sheet1 = book_excel.sheets()[0]
    book_num = sheet1.nrows
    row0_len=5#5列
    while(1):
        #输入书名
        bookname = input("请输入书名:")
        if(bookname == 'q'):
            return
        #查找
        for i in range(0,book_num):
            if(bookname == sheet1.cell(i,0).value):

                kucun=int(sheet1.cell_value(i,4))
                
                if(kucun>0):
                    (old_data,nrows)=read_old_data(row0_len)#5列
                    book_excel_w = xlwt.Workbook("图书.xls")
                    sheet2 = book_excel_w.add_sheet("books",cell_overwrite_ok=True)
                    #写入旧数据:
                    for n in range(nrows):
                        for j in range(row0_len):
                            sheet2.write(n,j,old_data[(n*row0_len)+j])
                    
                    print("借到了!")
                    sheet2.write(i,4,str(kucun-1))
                    print("剩余库存:",kucun-1)
                    book_excel_w.save("图书.xls")
                    break
    return


def main_window():
    while(1):
        print("\n====图书管理系统====")
        print("1.新书加入")
        print("2.书籍查询")
        print("3.图书借阅")
        print("========4退出=======")
        x= (input("请输入你的操作"))
        print('\n')
        if(x == '1'):
            new_book()
        elif(x=='2'):
            search()
        elif(x=='3'):
            borrow()
            
        elif(x=='4'):
            break
        else:
            print("输入无效")

    return


main_window()

加载全部内容

相关教程
猜你喜欢
用户评论