Introduction to the use of the xlrd module

1. Install the xlrd module

   Go to the python official website to download http://pypi.python.org/pypi/xlrd module installation, provided that the python environment has been installed.


2. Introduction

  1. Data types in common cells

    0 empty,1 string(text), 2 number, 3 date, 4 boolean, 5 error, 6 blank

  2. Import the module

      import xlrd

   3. Open the Excel file to read the data

       data = xlrd.open_workbook(filename)

   4. Commonly used functions

        The most important method in excel is the operation of book and sheet.


        1) Get a worksheet in the book

        table = data.sheets()[0] #Get by index order
        table = data.sheet_by_index(sheet_indx)) #Get by index order

        table = data.sheet_by_name(sheet_name)#Get by name

        The above three functions will return an xlrd.sheet.Sheet() object


        names = data.sheet_names() #Return the names of all worksheets in the book

        data.sheet_loaded(sheet_name or indx) # Check whether a sheet is imported

 

        2) row operations

         nrows = table.nrows #Get   the number of valid rows in the sheet

         table.row(rowx) #returns a list of all cell objects in the row

         table.row_slice(rowx) #returns a list of all cell objects in the column

         table.row_types(rowx, start_colx =0 , end_colx =None ) #returns a list consisting of the data types of all cells in the row

         table.row_values(rowx, start_colx =0 , end_colx =None ) #returns a list consisting of the data of all cells in the row

         table.row_len(rowx) #returns the effective cell length of the column


        3) Operation of column (colnum)

         ncols = table.ncols #Get the number of valid columns of the list

         table.col(colx, start_rowx =0 , end_rowx =None ) #returns a list of all cell objects in the column

         table.col_slice(colx, start_rowx =0 , end_rowx =None ) #returns a list of all cell objects in the column

         table.col_types(colx, start_rowx =0 , end_rowx =None ) #returns a list consisting of the data types of all cells in the column

         table.col_values(colx, start_rowx =0 , end_rowx =None ) #returns a list consisting of the data of all cells in the column


         4) Operations on cells

         table.cell(rowx,colx) #return the cell object

         table.cell_type(rowx,colx) #return the data type in the cell

         table.cell_value(rowx,colx) #Return the data in the cell

         table.cell_xf_index(rowx, colx) #I  haven't understood it yet

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324648776&siteId=291194637