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