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