基於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()
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- None Found