本文实例为大家分享了python实现图书管理系统的具体代码,供大家参考,具体内容如下
添加新书
查询
借阅
二次添加新书(读取已有的.xls并修改)
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
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() |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_45568186/article/details/115712025