During the recent testing process, I needed to use python to read excel use case data, so I went to understand and learn the xlrd library. Here we only record the operations related to reading excel data during use.
Install xlrd library(Recommended learning: Python video tutorial)
You can download the xlrd library package to Install locally or through the pip command. Here I choose the pip command:
pip install xlrd
Use xlrd to read excel data
For detailed operations, please refer to the xlrd library operation instruction document. The following are two A method to read excel data:
Read data according to the sheet name in Excel:
import xlrd def readExcelDataByName(fileName, sheetName): table = None errorMsg = None try: data = xlrd.open_workbook(fileName) table = data.sheet_by_name(sheetName) except Exception, msg: errorMsg = msg return table, errorMsg
According to the serial number of the sheet in Excel:
import xlrd def readExcelDataByIndex(fileName, sheetIndex): table = None errorMsg = "" try: data = xlrd.open_workbook(fileName) table = data.sheet_by_index(sheetIndex) except Exception, msg: errorMsg = msg return table, errorMsg
For more Python-related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to read values in excel in python. For more information, please follow other related articles on the PHP Chinese website!