Sunday, May 24, 2026

Python Material - Part - 30 - xlrd_Module

 __author__ = "Narendra Boyina"

# -----------------------------------------------------------------------------
# Copyright (c) 2025 BR Technologies PVT LTD
# -----------------------------------------------------------------------------
"""
Topics to be covered in today's class:
--> How to install xlwt module
--> purpose xlwt with example

"""

import sys, calendar, datetime,random,string,math

import xlwt
from xlwt import Workbook
import xlrd
from xlrd import open_workbook
import time
from datetime import datetime


# function definition
def write_data_to_excel():
# book = xlwt.Workbook(encoding="utf-8")
book = Workbook() # creating work book & it's instance

Raahi_sheet = book.add_sheet("Raahi") # creating excel sheet with Raahi name
Venkat_sheet = book.add_sheet("Venkat") # creating excel sheet with Venkat name
ganesh_sheet = book.add_sheet("ganesh") # creating excel sheet with ganesh name
meera_sheet = book.add_sheet("Meera") # creating excel sheet with Meera name
sheet5 = book.add_sheet("jasmin")


Raahi_sheet.write(0, 0, "Raahi faraz")
Raahi_sheet.write(1, 1, "I am Raahi")
Raahi_sheet.write(0, 4, "Hi")

ganesh_sheet.write(2,2, "Hi this is Ganesh")

current_time = time.ctime() # it will print time stamp

fmt_1 = xlwt.Style.easyxf("""
font: name Arial;
borders: left thick, right thick, top thick, bottom thick;
pattern: pattern solid, fore_colour red;
""")

Venkat_sheet.write(5, 5, current_time, fmt_1)

titles = ["Tag Name", "parameter_name", "state", "results"]
for i, j in enumerate(titles):
meera_sheet.write(2, i, j) # row, colum, value (2,0, "Tag Name") (2, 1, "parameter_name") (2, 2, "state")
fmt_3 = xlwt.Style.easyxf("""
font: name Arial;
borders: left thick, right thick, top thick, bottom thick;
pattern: pattern solid, fore_colour blue;
""")
for i, j in enumerate(titles):
sheet5.write(0, i, j, fmt_3) # row, colum, value, format

book.save("Narendra.xls")


# write_data_to_excel()

""" reading data from Excel workbook"""



def read_data_from_excel_file(path):
"""
Open and read an Excel file
"""
# book = xlrd.open_workbook(path)
book = open_workbook(path) # creating object for workbook (by using that object, can perform required operations)

print(book.nsheets) # print number of sheets
print(book.sheet_names()) # print sheet names

# get the first worksheet based on index
first_sheet = book.sheet_by_index(0) # Raahi will be selected.
print(first_sheet.row_values(1)) # can get the row based values
print(first_sheet.col_values(0)) # can get the colum based values

# get the first worksheet based on name
Meera_sheet = book.sheet_by_name("Meera")
print(Meera_sheet.row_values(2)) # can get the row based values # ['Tag Name', 'parameter_name', 'state', 'results']

# read a cell
Meera_sheet = book.sheet_by_name("Meera")
value = Meera_sheet.cell_value(2,2) # cell(row, column) # state
print(value)

if __name__ == "__main__":
path = r"C:\Users\nanarend\Downloads\Python_classes_May_2025\Class_37_xlrd_module\Narendra.xls "
read_data_from_excel_file(path)

Author: Boyina Narendra

Supporting Author: M. Meera Sindhu

Request: If you find this information useful, please provide your valuable comments

No comments:

Post a Comment