基于python实现图书管理系统
作者:昇柱
这篇文章主要为大家详细介绍了基于python实现图书管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。