Skip to content

Commit fc5c127

Browse files
committed
Added data_basefile
1 parent 730cccc commit fc5c127

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed

data_base.py

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
import openpyxl
2+
import os
3+
4+
def create_db(db_name, list_ot_headers):
5+
''' Creates the excel sheet and gives the values '''
6+
7+
def insert_value():
8+
for item in list_ot_headers:
9+
yield item
10+
table_generator = insert_value()
11+
12+
excel_file = openpyxl.Workbook()
13+
active_sheet = excel_file.active
14+
number_of_columns = len(list_ot_headers)
15+
for index in range(1, number_of_columns + 1):
16+
active_sheet.cell(row=1, column=index).value = next(table_generator)
17+
18+
excel_file.save(db_name +'.xlsx')
19+
print 'Database {} created successfully'.format(db_name)
20+
21+
22+
23+
24+
def insert_into_db(db_name, list_of_value):
25+
wb = openpyxl.load_workbook(db_name)
26+
active_sheet = wb.active
27+
column = active_sheet.max_column
28+
29+
# def data_generator():
30+
# for item in list_of_value:
31+
# yield item
32+
my_gen = (item for item in list_of_value)
33+
34+
if len(list_of_value) % column != 0:
35+
raise Exception('Make sure the data has correct length')
36+
37+
outer_loop_counter = len(list_of_value)/ column
38+
for counter in range(outer_loop_counter):
39+
row = active_sheet.max_row
40+
for i in range(1, column+1):
41+
active_sheet.cell(row=row+1, column=i).value = next(my_gen)
42+
43+
wb.save(db_name)
44+
print 'Successfully inserted data'
45+
46+
47+
48+
49+
50+
def delete_from_db(db_name, list_of_value, field='first_column'):
51+
wb = openpyxl.load_workbook(db_name)
52+
active_sheet = wb.active
53+
headers = show_tables(db_name)
54+
if field in headers:
55+
column_value = headers.index(field) + 1
56+
row, col = active_sheet.max_row, active_sheet.max_column
57+
match_results = []
58+
for i in range(1,row+1):
59+
if active_sheet.cell(row=i, column=column_value).value == list_of_value:
60+
match_results.append((i,column_value))
61+
if len(match_results) > 1:
62+
print '''There are more than one match data in database. Which one do you want to delete?
63+
Press the required number. '''
64+
col_into_letter = '{}'.format(openpyxl.utils.get_column_letter(col))
65+
match_column_slice = ''
66+
for index,i in enumerate(match_results, start=1):
67+
s = ''
68+
match_column_slice = 'A{}:{}{}'.format(i[0], col_into_letter, i[0] )
69+
for match in active_sheet[match_column_slice]:
70+
for item in match:
71+
s = s + ' ' + item.value
72+
print '{}. {}'.format(index, s)
73+
74+
try:
75+
prm = input('Enter the number to delete item >')
76+
if not 1 <= prm <= len(match_results):
77+
raise Exception('Enter a valid number')
78+
except (NameError, TypeError):
79+
raise Exception('Enter a valid number')
80+
81+
list_index_to_delete = match_results[prm - 1 ]
82+
column_slice_to_delete = list_index_to_delete[0]
83+
slice_val = 'A{0}:{1}{0}'. format(column_slice_to_delete, col_into_letter)
84+
for item in active_sheet[slice_val]:
85+
for i in item:
86+
i.value = None
87+
print 'Successfully deleted !!'
88+
89+
else:
90+
print 'Found one match'
91+
for i in match_results:
92+
match_column_slice = 'A{0}:{1}{0}'.format(i[0], col_into_letter)
93+
for match in active_sheet[match_column_slice]:
94+
for each_tup in match:
95+
each_tup.value = None
96+
print 'Deleted value'
97+
98+
else:
99+
print 'No match found!'
100+
101+
wb.save(db_name)
102+
103+
104+
105+
def update_from_db(db_name, old_data, data_to_be_updated):
106+
wb = openpyxl.load_workbook(db_name)
107+
active_sheet = wb.active
108+
row, col = active_sheet.max_row, active_sheet.max_column
109+
old_data = old_data.split(',')
110+
od = ''.join(old_data)
111+
112+
new_data = data_to_be_updated.split(',')
113+
114+
replace_new_data = (item for item in new_data)
115+
116+
if not len(old_data) == len(new_data) == col:
117+
raise Exception('Make sure the data length is correct.')
118+
match_slice = ''
119+
for r in range(2,row+1):
120+
s = ''
121+
for c in range(1,col+1):
122+
val = active_sheet.cell(row=r, column=c).value
123+
if val is None:
124+
continue
125+
s = s + ' ' + active_sheet.cell(row=r, column=c).value
126+
127+
if s.strip() == od.strip():
128+
match_slice = 'A{0}:D{0}'.format(r)
129+
130+
for item in active_sheet[match_slice]:
131+
for each_tup in item:
132+
each_tup.value = next(replace_new_data)
133+
print each_tup.value
134+
print 'Successfully updated data'
135+
136+
wb.save(db_name)
137+
138+
update_from_db('Myfile.xlsx', 'deepak, am, Yo-kyla, ED', 'deepakPanta, PM, Finland, Oh.yah')
139+
140+
def search_data(db_name, search_keywords, field):
141+
wb = openpyxl.load_workbook(db_name)
142+
active_sheet = wb.active
143+
headers = show_tables(db_name)
144+
if field in headers:
145+
column_value = headers.index(field) + 1
146+
row, col = active_sheet.max_row, active_sheet.max_column
147+
match_results = []
148+
for i in range(1,row+1):
149+
if active_sheet.cell(row=i, column=column_value).value == search_keywords:
150+
match_results.append((i,column_value))
151+
152+
print ' Your search for {} gives this result'.format(search_keywords)
153+
col_into_letter = '{}'.format(openpyxl.utils.get_column_letter(col))
154+
match_column_slice = ''
155+
for index,i in enumerate(match_results, start=1):
156+
s = ''
157+
match_column_slice = 'A{}:{}{}'.format(i[0], col_into_letter, i[0] )
158+
for match in active_sheet[match_column_slice]:
159+
for item in match:
160+
s = s + ' ' + item.value
161+
print '{}. {}'.format(index, s)
162+
163+
def show_db():
164+
data_bases = []
165+
for files in os.listdir('.'):
166+
if files.endswith('xlsx'):
167+
filename, _ = files.split('.')
168+
data_bases.append(filename)
169+
if len(data_bases) != 0:
170+
print 'Found following database\n'
171+
for item in data_bases:
172+
print item
173+
else:
174+
print 'Found no database with that name.'
175+
176+
177+
def sort_db_content(db_name, field):
178+
pass
179+
180+
def show_tables(db_name):
181+
wb = openpyxl.load_workbook(db_name)
182+
sheet = wb.active
183+
number_of_columns = sheet.max_column
184+
headers = []
185+
#print 'The tables in {} are as follows: '.format(db_name)
186+
for i in range(1, number_of_columns + 1):
187+
column_head = sheet.cell(row=1, column=i).value
188+
#print '{}. {}'.format(i,column_head)
189+
headers.append(column_head)
190+
191+
return headers
192+
193+
194+
195+
196+
197+
#search_data('Myfile.xlsx', 'Jenga', 'First Name')
198+
#delete_from_db('Myfile.xlsx', 'Jenga', 'First Name')
199+
#show_tables('Myfile.xlsx')
200+
#create_db('Myfile', ['First Name', 'Last Name', 'Age', 'Score'])
201+
#insert_into_db('Myfile.xlsx', ['deepak', 'am', 'Yo-kyla', 'ED', 'Jenga', 'Koila', 12,'deepak'])
202+
#show_db()

0 commit comments

Comments
 (0)