|
| 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 |
1 | 4 |
|
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 |
5 | 5 |
|
6 |
| - |
7 |
| -#download and install openpyxl |
| 6 | +# download and install openpyxl |
8 | 7 | import openpyxl
|
9 | 8 | 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) |
12 | 11 | sheet_obj = wb_obj.active
|
13 | 12 |
|
14 | 13 |
|
15 | 14 | print("if you want to get value from cell type OUT")
|
16 | 15 | if input() == "OUT":
|
17 | 16 | print("remember that cells start from 1,1")
|
18 | 17 | 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) |
22 | 21 |
|
23 | 22 |
|
24 | 23 | 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) |
33 | 31 | print("if you want to create a new excel file, print NEW")
|
34 |
| -if input()=="NEW": |
| 32 | +if input() == "NEW": |
35 | 33 | print("enter your workbook name eg name.xlsx")
|
36 |
| - s=input() |
| 34 | + s = input() |
37 | 35 | from openpyxl import Workbook
|
38 | 36 | wbk = Workbook()
|
39 | 37 | wbk.save(filename=s)
|
40 |
| - sheet=wbk.active |
41 |
| - e="" |
42 |
| - while e!="NO": |
| 38 | + sheet = wbk.active |
| 39 | + e = "" |
| 40 | + while e != "NO": |
43 | 41 | 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())) |
45 | 43 | 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) |
48 | 46 | v.value = t
|
49 | 47 | wbk.save(s)
|
50 | 48 | 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": |
55 | 52 | print("enter range to be merged like C1:F6")
|
56 |
| - p=input() |
57 |
| - sheet = wbk.active |
| 53 | + p = input() |
| 54 | + sheet = wbk.active |
58 | 55 | sheet.merge_cells(p)
|
59 | 56 | wbk.save(s)
|
60 |
| - |
61 |
| - |
62 | 57 | print("to copy from one file to another type COPY")
|
63 |
| -if input()=="COPY": |
| 58 | +if input() == "COPY": |
64 | 59 | print("enter source file address")
|
65 |
| - filename=input() |
| 60 | + filename = input() |
66 | 61 | wb1 = openpyxl.load_workbook(filename)
|
67 | 62 | ws1 = wb1.worksheets[0]
|
68 | 63 | print("enter destination file address")
|
69 |
| - filename1 =input() |
| 64 | + filename1 = input() |
70 | 65 | wb2 = openpyxl.load_workbook(filename1)
|
71 | 66 | 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 |
76 | 71 | wb2.save(str(filename1))
|
77 |
| - |
78 |
| - |
79 | 72 | 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 |
82 | 75 | 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") |
85 | 78 | for i in range(l):
|
86 |
| - b=int(input()) |
| 79 | + b = int(input()) |
87 | 80 | 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) |
90 | 83 | chart = BarChart3D()
|
91 | 84 | chart.add_data(values)
|
92 | 85 | print("enter chart title")
|
93 |
| - title=input() |
| 86 | + title = input() |
94 | 87 | chart.title = title
|
95 | 88 | chart.x_axis.title = " X AXIS "
|
96 | 89 | chart.y_axis.title = " Y AXIS "
|
97 | 90 | sheet.add_chart(chart, "E2")
|
98 | 91 | 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