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