The use of xlrd in Python learning

Source: Internet
Author: User

Recently, I want to complete an automated framework that uses Excel tables to manage test cases, so I re-learned the next xlrd, using this module to read data from Excel tables.

First, install the XLRD

Download http://pypi.python.org/pypi/xlrd on the Python website, unzip it, go to the unpacked directory, and use the Python setup.py install directly to Python Shell Test Import XLRD If no exception is thrown, the installation is successful

Second, XLRD module use

1. Import xlrd

Import xlrd

2. Before you operate Excel, open the Excel file you want to manipulate

Data=xlrd.open_workbook (' myexcel.xlsx ')

3. Get Worksheets (three ways)

Table=data.sheets () [0] #通过索引顺序获取
Table=data.sheet_by_index (1) #通过索引顺序获取
Table=data.sheet_by_name (U ' test case ') #通过Excel表名称

4. Get the whole row and column (array)

table.row_values (n) #获取第nLinetable.col_values (n) #获取第n列5. Read the value of a cell in the table (Table.cell (I,J). Value)

nrow=table.nrows# the number of rows to read from an Excel table
ncol=table.ncols# the number of columns to read the table
#print Nrow,ncol
Testcase=[] #筛选出要进行测试的用例
For I in Range (Nrow):
If Table.cell (i,1). Value = = U ' is ':
Testcase.append (Table.cell (i,0). Value) #table. Cell (i,0). Value for the 1th row of the first i+1 row

6. Read with row and column index

Print Table.row (0) [1].value #打印第一行, Value of 2nd column

Print Table.col (0) [1].value #打印一列, value of line 2nd

Details can be viewed: http://blog.chinaunix.net/uid-21222282-id-3420444.html

The use of xlrd in Python learning

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.