Fundamentals 3 min read

Using xlrd to Read Excel Files in Python

xlrd is a Python module for reading Excel files, and this guide explains how to install it, locate its documentation, and demonstrates basic operations such as opening a workbook, accessing sheets, retrieving rows, columns, and cell values with example code snippets.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Using xlrd to Read Excel Files in Python

xlrd is a Python module used to read Excel files. It can be installed directly via pip or through PyCharm.

Download links: PyPI , official site python-excel.org , documentation online , PDF download .

Below is a basic example demonstrating how to open an Excel file, retrieve sheet names, access sheets by name or index, and read rows, columns, and cell values.

# encoding: utf-8
import xlrd

# Open the Excel file
xlsfile = r'/Users/xxx/Desktop/工作簿1.xlsx'
book = xlrd.open_workbook(xlsfile)

# Get sheet object
sheet_name = book.sheet_names()[0]
print(sheet_name)
sheet1 = book.sheet_by_name(sheet_name)
sheet0 = book.sheet_by_index(0)

# Get number of rows and columns
nrows = sheet.nrows
ncols = sheet.ncols

# Get row and column values
row_data = sheet.row_values(0)
col_data = sheet.col_values(0)

# Get cell values
cell_value1 = sheet.cell_value(0,1)
print(cell_value1)
cell_value2 = sheet.cell(0,1)
print(cell_value2)

For more automation learning resources, follow the QR code provided.

Data ProcessingTutorialExcelxlrd
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.