Home  >  Article  >  Backend Development  >  How to read values ​​in excel in python

How to read values ​​in excel in python

(*-*)浩
(*-*)浩Original
2019-07-01 10:34:347212browse

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.

How to read values ​​in excel in python

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn