Read data From Excel Python
- crystgandhi
- Feb 8, 2024
- 1 min read
import openpyxl
# Load the Excel workbook
workbook = openpyxl.load_workbook("C://Users/Nikil/PycharmProjects/pythonBasics/Userdata.xlsx")
# Access the specified sheet by name (using workbook[sheet_name])
sheet = workbook["Sheet1"]
# Get the number of rows and columns in the sheet
rows = sheet.max_row
columns = sheet.max_column
print("Number of rows:", rows)
print("Number of columns:", columns)
# Iterate through each row and column to print cell values
for r in range(2, rows + 1):
# Start from the second row (assuming headers in the first row)
for c in range(1, columns + 1):
# Print the value of each cell, separated by spaces
print(sheet.cell(row=r, column=c).value, end=" ")
# Move to the next line after printing values of all columns in the current row
print()
Kommentare