基于python实现图书管理系统
本文实例为大家分享了python实现图书管理系统的具体代码,供大家参考,具体内容如下
添加新书
查询
借阅
二次添加新书(读取已有的.xls并修改)
代码:
import xlwtimport xlrddef 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,nrowsdef 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+1book=[]#清空book缓存 returndef 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=1print('书名:',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('查无此书。') returndef 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 returndef 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’): breakelse: print('输入无效') returnmain_window()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。
相关文章: