Python_xlrd and xlwt module introduction

Summary:

xlrd in Python and xlwt are two very important module, mainly to solve the problem of Python interfaces to read and write data to excel.

 

A. Xlrd module

excel format data may be read 1.1 xlrd there are seven:

  • empty (empty)
  • text(string)
  • number,
  • date,
  • boolean,
  • error,
  • blank (blank form)

 

1.2 Important functions:

A. Getting Book Workbook (ie excel workbook contains all the worksheets)

 

Data =. 1 xlrd.open_workbook (filename)    
    # reading workbook called filename; 
    Excel file # Python and if the read program in the same path, the filename of the file name string;
    # if the read excel file with the Python program is not under the same path, the path and filename for the file name, then the path to r plus a native character.    
    # Return value data type xlrd.book.Book, which excel spreadsheet workbooks comprising all the read. More ways: names = data.sheet_names () # returns the names of all the worksheets in the book of data.sheet_loaded (sheet_name or indx) # check whether a sheet import is complete

 

 

 

B. Sheet acquisition sheet (i.e., in a table Book)

= Table data.sheets () [0]   
 # acquired by the index order 

Table = data.sheet_by_index (sheet_indx))   
     # obtained by index order 

Table = data.sheet_by_name (SHEET_NAME)  
     # obtained by Title 

    # three methods return values are xlrd .sheet.Sheet () Object

 

 

 

C. Operation worksheet rows sheet (sheet object table)

. 1 nrows = table.nrows  
 2      # Get the number of rows in the sheet, injection, table.nrows later herein without (). 
. 3  
. 4  table.row (rowx)
 . 5      # returned by the cells of all row of objects list, which tabel.raw () method does not distinguish. 
. 6  
. 7  table.row_slice (rowx)  
 . 8      # returns a list of all the columns of the cell objects 
. 9  
10 table.row_types (rowx, start_colx = 0, end_colx = None)    
 . 11      # returned by all cells in the row of a list of data types; 
    # return value is a list of logic values, if the type is empy and 0 otherwise. 1
12 is 13 is table.row_values (rowx, start_colx = 0, end_colx = None) 14 # returns all cells in that row column table of data consisting of 15 16 table.row_len (rowx) . 17 # Returns the effective column length of the cell, i.e. the row number of data

 

 

 

D. Operation sheet worksheet columns (table object sheet)

. 1 ncols = table.ncols   
 2      # obtain a list of valid columns 
. 3  
. 4 table.col (COLX, start_rowx = 0, end_rowx = None)  
 . 5      # list returned by the column, all cells of the objects 
. 6  
. 7 table.col_slice (COLX, start_rowx = 0, end_rowx = None)  
 . 8      # returns a list of all the columns of the cell objects 
. 9  
10 table.col_types (COLX, start_rowx = 0, end_rowx = None)    
 . 11      # returns all of the columns list data types of cells is 
12 is  
13 is table.col_values (COLX, start_rowx = 0, end_rowx = None)  
 14      # returns a list of all the data cells in that column composed of    

 

 

 

E. cell operation (sheet object table)

. 1  table.cell (rowx, COLX)   
 2      # returns the cell object 
. 3  
. 4  table.cell_type (rowx, COLX)    
 . 5      # data types to return the corresponding position in the cell 
. 6  
. 7  table.cell_value (rowx, COLX)   
 . 8      # returns the corresponding data cell position in the 
9     

Two. Xlwt module

 

Guess you like

Origin www.cnblogs.com/carreyBlog/p/11616396.html