Skip to content

Commit bc9b5a9

Browse files
authored
Update excel-tools.py
1 parent 8e35c2b commit bc9b5a9

File tree

1 file changed

+44
-71
lines changed

1 file changed

+44
-71
lines changed

excel_tools/excel-tools.py

Lines changed: 44 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,91 @@
1+
# python script that deals with basic excel operations
2+
# it is better to keep the excel file in the same folder
3+
# enter full address carefully if you are doing it manually
14

2-
#python script that deals with basic excel operations
3-
#it is better to keep the excel file in the same folder
4-
#enter full address carefully if you are doing it manually
55

6-
7-
#download and install openpyxl
6+
# download and install openpyxl
87
import openpyxl
98
print("enter the name of the excel file")
10-
path=input()
11-
wb_obj = openpyxl.load_workbook(path)
9+
path = input()
10+
wb_obj = openpyxl.load_workbook(path)
1211
sheet_obj = wb_obj.active
1312

1413

1514
print("if you want to get value from cell type OUT")
1615
if input() == "OUT":
1716
print("remember that cells start from 1,1")
1817
print("enter row, column eg 5,1")
19-
m,n=list(map(int,input().split()))
20-
cell_obj = sheet_obj.cell(row = m, column = n)
21-
print("the value of the given cell is: ",cell_obj.value)
18+
m, n = list(map(int, input().split()))
19+
cell_obj = sheet_obj.cell(row=m, column=n)
20+
print("the value of the given cell is: ", cell_obj.value)
2221

2322

2423
print("if you want the entire excel file displayed type OUTX")
25-
if input()=="OUTX":
26-
row = sheet_obj.max_row
27-
column = sheet_obj.max_column
28-
for i in range(1, row + 1):
29-
for j in range(1, column + 1):
30-
cell_obj = sheet_obj.cell(row = i, column = j)
31-
print(cell_obj.value)
32-
24+
if input() == "OUTX":
25+
row = sheet_obj.max_row
26+
column = sheet_obj.max_column
27+
for i in range(1, row + 1):
28+
for j in range(1, column + 1):
29+
cell_obj = sheet_obj.cell(row=i, column=j)
30+
print(cell_obj.value)
3331
print("if you want to create a new excel file, print NEW")
34-
if input()=="NEW":
32+
if input() == "NEW":
3533
print("enter your workbook name eg name.xlsx")
36-
s=input()
34+
s = input()
3735
from openpyxl import Workbook
3836
wbk = Workbook()
3937
wbk.save(filename=s)
40-
sheet=wbk.active
41-
e=""
42-
while e!="NO":
38+
sheet = wbk.active
39+
e = ""
40+
while e != "NO":
4341
print("enter row, column of cell in which you want to input data")
44-
a,b=list(map(int,input().split()))
42+
a, b = list(map(int, input().split()))
4543
print("enter value")
46-
t=input()
47-
v=sheet.cell(row = m, column = n)
44+
t = input()
45+
v = sheet.cell(row=m, column=n)
4846
v.value = t
4947
wbk.save(s)
5048
print("type NO to exit, YES to repeat")
51-
e=input()
52-
53-
print("type MERGE to merge desired cells")
54-
if input()=="MERGE":
49+
e = input()
50+
print("type MERGE to merge desired cells")
51+
if input() == "MERGE":
5552
print("enter range to be merged like C1:F6")
56-
p=input()
57-
sheet = wbk.active
53+
p = input()
54+
sheet = wbk.active
5855
sheet.merge_cells(p)
5956
wbk.save(s)
60-
61-
6257
print("to copy from one file to another type COPY")
63-
if input()=="COPY":
58+
if input() == "COPY":
6459
print("enter source file address")
65-
filename=input()
60+
filename = input()
6661
wb1 = openpyxl.load_workbook(filename)
6762
ws1 = wb1.worksheets[0]
6863
print("enter destination file address")
69-
filename1 =input()
64+
filename1 = input()
7065
wb2 = openpyxl.load_workbook(filename1)
7166
ws2 = wb2.active
72-
for i in range (1, row + 1):
73-
for j in range (1, column + 1):
74-
c = ws1.cell(row = i, column = j)
75-
wb2.cell(row = i, column = j).value = c.value
67+
for i in range(1, row + 1):
68+
for j in range(1, column + 1):
69+
c = ws1.cell(row=i, column=j)
70+
wb2.cell(row=i, column=j).value = c.value
7671
wb2.save(str(filename1))
77-
78-
7972
print("if you want to create a bar chart type CHART")
80-
if input()=="CHART":
81-
from openpyxl.chart import BarChart3D,Reference
73+
if input() == "CHART":
74+
from openpyxl.chart import BarChart3D, Reference
8275
print("how many columns do you want")
83-
l=int(input())
84-
print("enter",l, " values")
76+
l = int(input())
77+
print("enter", l, " values")
8578
for i in range(l):
86-
b=int(input())
79+
b = int(input())
8780
sheet.append([b])
88-
values = Reference(sheet, min_col = 1, min_row = 1,
89-
max_col = 1, max_row = l)
81+
values = Reference(sheet, min_col=1, min_row=1,
82+
max_col=1, max_row=l)
9083
chart = BarChart3D()
9184
chart.add_data(values)
9285
print("enter chart title")
93-
title=input()
86+
title = input()
9487
chart.title = title
9588
chart.x_axis.title = " X AXIS "
9689
chart.y_axis.title = " Y AXIS "
9790
sheet.add_chart(chart, "E2")
9891
wbk.save("BarChart.xlsx")
99-
100-
101-
102-
103-
104-
105-
106-
107-
108-
109-
110-
111-
112-
113-
114-
115-
116-
117-
118-

0 commit comments

Comments
 (0)