-
Notifications
You must be signed in to change notification settings - Fork 0
/
xlsx_file.py
116 lines (60 loc) · 1.99 KB
/
xlsx_file.py
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
#!/usr/bin/env python
# coding: utf-8
# In[55]:
import xlrd
from xlrd import open_workbook
# In[56]:
loc = ("excel.xlsx")
# In[60]:
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(5,4)
# In[62]:
for i in range(0,sheet.nrows):
for j in range(0,sheet.ncols):
print(sheet.cell_value(i, j))
# In[70]:
# Reading and writing in excel can be done by single module
import openpyxl
from openpyxl.utils.cell import get_column_letter
workbook = openpyxl.load_workbook('excel.xlsx')
workbook.sheetnames
worksheet = workbook["Sheet1"]
# In[71]:
# Number of rows
number_of_rows = worksheet.max_row
# Number of columns
number_of_columns = worksheet.max_column
# In[72]:
word=input('Enter the word you want to replace :')
new_word=input("Enter the replaced word : ")
replacementTextKeyPairs ={'{}'.format(word):'{}'.format(new_word)}
#print(replacementTextKeyPairs)
# In[73]:
for i in range(number_of_columns):
for k in range(number_of_rows):
cellValue = str(worksheet[get_column_letter(i+1)+str(k+1)].value)
for key in replacementTextKeyPairs.keys():
if str(cellValue) == key:
newCellValue = replacementTextKeyPairs.get(key)
worksheet[get_column_letter(i+1)+str(k+1)] = str(newCellValue)
# In[74]:
workbook.save('sampleexcelwithreplacedtextusingopenpyxl.xlsx')
# In[75]:
loc1 = ("sampleexcelwithreplacedtextusingopenpyxl.xlsx")
wb1 = xlrd.open_workbook(loc1)
sheet1 = wb1.sheet_by_index(0)
for i in range(0,sheet1.nrows):
for j in range(0,sheet1.ncols):
print(sheet1.cell_value(i, j))
# In[83]:
word_search=input('Enter the word you want to search :')
count=0
for i in range(0,sheet.nrows):
for j in range(0,sheet.ncols):
if sheet.cell_value(i,j) == word_search:
print("Word Found at the position ",i,'*',j)
count=count+1
if count==0:
print("Word is not Found")
# In[ ]: