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

How to read values ​​in excel in python

(*-*)浩
Release: 2019-07-11 13:43:56
Original
7345 people have browsed it

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
Copy after login

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
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template