xlrd module learning in python

1. The xlrd module is mainly used for reading excel forms, supporting xlsx and xls; xlwt is mainly used for writing excel and reading libraries

2. Data types in commonly used cells

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

use

1. Import module

import xlrd

Second, open the excel file to read the data (the file is created first)

 

data = xlrd.open_workbook ('study.xls') #File name and path, if the path or file name has Chinese, add an r to the front to indicate the native character; for example, 'rLearn.xls'

 

Three, xlrd module operation

1. Basic functions

1.1. Open the workbook to get the Book object
  • xlrd.open_workbook (filename [, logfile, file_contents, ...]): open the excel file
    filename: the name of the file to be operated (including the file path and file name);

If filename does not exist, an error FileNotFoundError is reported;
if filename exists, the return value is the xlrd.book.Book object

1.2. Get all sheet names in Book object
  • BookObject.sheet_names (): Get the names of all sheets and display them in a list
1.3. Get all Sheet objects in Book object
  • BookObject.sheets (): Get all sheet objects and display them as a list

  • BookObject.sheet_by_index (sheetx): Get the desired sheet object
    sheetx as the index value through the sheet index , and the index starts from 0;
    if sheetx exceeds the index range, an Error IndexError is reported;
    if sheetx is within the index range, the return value is xlrd.sheet .Sheet object

  • BookObject.sheet_by_name (sheet_name): Get the desired sheet object by sheet name
    sheet_name is the sheet name;
    if sheet_name does not exist, an error xlrd.biffh.XLRDError is reported;
    if sheet_name exists, the return value is the xlrd.sheet.Sheet object

1.4. Determine whether a sheet in the Book object is imported
  • BookObject.sheet_loaded (sheet_name_or_index): Determine whether the sheet is successfully imported by the sheet name or index. The
    return value is bool. If the return value is True, it means that it has been imported; if the return value is False, it has not been imported.
1.5. Manipulate rows in Sheet object
  • SheetObject.nrows: Get the number of valid rows in a sheet

  • SheetObject.row_values ​​(rowx [, start_colx = 0, end_colx = None]): Get the data from the start_colx column to the end_colx column in the rowx + 1 row of the sheet, and the return value is a list.
    If rowx is in the index range, return the data in the form of a list;
    if rowx is not in the index range, an Error IndexError is reported

  • SheetObject.row (rowx): Get the rowx + 1 row unit in the sheet, the return value is a list; the
    content of each value in the list is: Unit type: unit data

  • SheetObject.row_slice (rowx [, start_colx = 0, end_colx = None]): get the unit of rowx + 1 row in the sheet from start_colx column to end_colx column in slice mode, the return value is a list; the
    content of each value in the list is: unit Type: Unit data

  • SheetObject.row_types (rowx [, start_colx = 0, end_colx = None]): Get the cell type of rowx + 1 row in the sheet from start_colx column to end_colx column, the return value is array.array type.
    Unit type ctype: empty is 0, string is 1, number is 2, date is 3, boolean is 4, error is 5 (left is the type, right is the corresponding value of the type);

  • SheetObject.row_len (rowx): Get the length of rowx + 1 row in sheet

rowx: row index, the number of rows starts from 0 (0 means the first row), required parameters;
start_colx: start column, means to take values ​​from the start_colx column, including the value of the start_colx;
end_colx: end column, means to End_colx column end value, excluding the end_colx value;

start_colx defaults to 0, end_colx defaults to None: means to take the entire row of related data;

  • SheetObject.get_rows (): Get a generator for all rows of a sheet
1.6. Column operations in Sheet object
  • SheetObject.ncols: Get the number of valid columns in a sheet

  • SheetObject.col_values ​​(self, colx [, start_rowx = 0, end_rowx = None]): Get the data from the start_rowx row to the end_rowx row of the colx + 1 column in the sheet, and the return value is a list.

  • SheetObject.col_slice (colx [, start_rowx = 0, end_rowx = None]): Obtain the data from the start_rowx row to the end_rowx row of the colx + 1 column of the sheet in a slice mode, and the return value is a list.
    The contents of each value in the list are: Unit type: Unit data

  • SheetObject.col_types (colx [, start_rowx = 0, end_rowx = None]): Get the cell type of the colx + 1 column in the sheet from start_rowx row to end_rowx row, the return value is a list

1.7. Perform operations on the cells of the Sheet object
  • ShellObeject.cell (rowx, colx): Get the cell object in rowx + 1 row and colx + 1 column of the sheet object, the return value is of type 'xlrd.sheet.Cell', and the format of the returned value is "cell type: cell value".

  • ShellObject.cell_value (rowx, colx): Get the cell data of rowx + 1 row and colx + 1 column in the sheet object, and the return value is the type of the current value (such as float, int, string ...);

  • ShellObject.cell_type (rowx, colx): Get the cell data type value of rowx + 1 row and colx + 1 column in the sheet object;
    cell type ctype: empty is 0, string is 1, number is 2, date is 3, boolean is 4, error is 5;

Specific operation:
import xlrd 
'' 'Open exce table' ''
wook_book = xlrd.open_workbook ('study.xls')
print (wook_book) #jResult: <xlrd.book.Book object at 0x000001DC91214B50>


' '' 1, get all sheet names '' '
sheet_names = wook_book.sheet_names ()
print (sheet_names) #Result: List [' people ',' animal ',' car ']



' '' 2, Get all or a certain sheet object '' '
' '' Get All sheet objects' ''
sheet_object = wook_book.sheets ()
print (sheet_object) #RESULTS [<xlrd.sheet.Sheet object at
# 0x0000026F491C21C0>,
# <xlrd.sheet.Sheet object at 0x0000026F491C22E0>,
# <xlrd.sheet .Sheet object at 0x0000026F491C22B0>] #Get the
first sheet object by index
sheet1_object = wook_book.sheet_by_index (0)
print (sheet1_object) #Get the

first sheet object by name
sheet2_object = wook_book.sheet_by_name ('people')
print (sheet2_object)



'' '3. Determine whether a sheet has been imported' '' #Check
whether sheet1 is imported into
sheet1_is_load by index = wook_book.sheet_loaded (sheet_name_or_index = 0)
print (sheet1_is_load) #Result: True

# Determine whether sheet1 is imported by sheet name
sheet1_name_is_load = wook_book.sheet_loaded (sheet_name_or_index = 'people')
print (sheet1_name_is_load) #The result is True or False




'' '4. Perform operations on the sheet object: such as the number of valid rows, the data of a row from column n1 to n2, the unit and type of a row, and the length of a row. . . '' ' #Get the
number of valid rows in sheet1 nrows: the number of rows
nrows = sheet1_object.nrows
print (nrows) #get

the data of the third row in sheet1
all_row_values ​​= sheet1_object.row_values ​​(rowx = 2)
print (all_row_values) #get
some The value of the first few columns to the first few rows in the row
row_values ​​= sheet1_object.row_values ​​(rowx = 2, start_colx = 1, end_colx = 3)
print (row_values) #Get

the unit object in the third row of sheet1, which is the unit object
row_object = sheet1_object.row (rowx = 2)
print (row_object) #Get
the unit of row 3 in sheet1
row_slice = sheet1_object.row_slice (rowx = 2)
print (row_slice) #Get

the unit type of row 3 in sheet1
row_type = sheet1_object.row_types (rowx = 2)
print (row_type) #Get

the length of the third row in sheet1
row_len = sheet1_object.row_len (rowx = 2)
print (row_len) #Get

the generator of all rows in sheet1
row_generator = sheet1_object.get_rows ()
print (row_generator)




'' '5. Perform operations on the columns in the sheet object' '' #Get the 
number of valid columns ncols in sheet1: the number of columns
ncols = sheet1_object.ncols
print (ncols)
#get the data in column 1 of sheet1
col_values ​​= sheet1_object.col_values ​​(colx = 1)
print (col_values)
col_value1 = sheet1_object.col_values ​​(1,1,3)
print (col_value1) #Get
the cell in the second column of sheet1
col_silce = sheet1_object.col_slice (colx = 1)
print (col_silce ) #Get
the cell type of the second column in sheet1
col_types = sheet1_object.col_types (colx = 1)
print (col_types)
'' '5. Perform operations on the cells in the sheet object' '' 
#Get the cell object in row + 1 row and colx + 1 column in
sheet1 cell_info = sheet1_object.cell (rowx = 1, colx = 2)
print ( cell_info)
print (type (cell_info)) # <class 'xlrd.sheet.Cell'>

#Get the cell value of row + 1 row and column colx + 1 in
sheet1 cell_value = sheet1_object.cell_value (rowx = 1, colx = 2)
print (cell_value)

#Get the cell type value of row + 1 row and column colx + 1 in sheet sheet
cell_type = sheet1_object.cell_type (rowx = 1, colx = 2)
print (cell_type)
 

 

 

Guess you like

Origin www.cnblogs.com/aichixigua12/p/12672755.html