-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcel_read.py
More file actions
27 lines (22 loc) · 960 Bytes
/
excel_read.py
File metadata and controls
27 lines (22 loc) · 960 Bytes
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
import openpyxl
def readExcel():
# Defining the name of the file statically
path = "py-read.xlsx"
# Opening the active sheet of the excel sheet
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active
cell_obj = sheet_obj.cell(row=1, column=1)
# Print total rows and columns in the sheet
m_rows = sheet_obj.max_row
m_columns = sheet_obj.max_column
print(f"There are total : {m_rows} rows in the current sheet {sheet_obj}")
print(f"There are total : {m_columns} columns in the current sheet {sheet_obj}")
#Fetching name of the headings
for i in range(1, m_columns+1):
cell_obj = sheet_obj.cell(row = 1, column = i)
print(cell_obj.value, end = " ")
for i in range(1, m_columns+1): #Traversing all columns
print(f"\nPrinting column {i}\n")
for j in range(1, m_rows+1): # Traversing all rows
cell_obj = sheet_obj.cell(row=j, column=i)
print(cell_obj.value)