Python_Commonly used Excel third-party libraries_xlrd

1. Installation
Go to the python official website to download http://pypi.python.org/pypi/xlrd module installation, provided that the python environment has been installed.
Or in the cmd window pip install xlrd
2. Use introduction
2.1. Data types in common cells
0 empty (empty), 1 string (text), 2 number, 3 date, 4 boolean (Boolean), 5 error, 6 blank
2.2. Import module and open file
import xlrd
data = xlrd.open_workbook (filename)
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.
2.3, commonly used functions

1) Sheet operation
table = data.sheets () [0]
#Get table by index order = data.sheet_by_index (sheet_indx)) #Get by index order
If sheet_indx is out of the index range, then report Error IndexError;
if sheet_indx is in the index range, The return value is the xlrd.sheet.Sheet object.
table = data.sheet_by_name (sheet_name) #Get by name
If sheet_name does not exist, an error xlrd.biffh.XLRDError will be reported;
if sheet_name exists, the return value will be the xlrd.sheet.Sheet object.
The above three functions will return an xlrd.sheet.Sheet () object
names = data.sheet_names () #return the names of all worksheets in the book, and display
them in a list. After the import is complete, the
return value is of type bool. If the return value is True, it means that it has been imported; if the return value is False, it means that it has not been imported.

2) Row operation
rowx: row label, row count 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, indicating that the value is taken to the end of the end_colx column, excluding the value of the end_colx;

nrows = table.nrows #Get the number of valid rows in the sheet
table.row (rowx)
#Return a list of all cell objects in the row table.row_slice (rowx) #Return all the cells in the column A list of objects
table.row_types (rowx, start_colx = 0, end_colx = None) #Return the data from the start_colx column to the end_colx column in the rowx + 1 row of the sheet, the return value is the list
table.row_values ​​(rowx, start_colx = 0, end_colx = None)
#Return a list consisting of the data of all cells in the row table.row_len (rowx) #Return the effective cell length of the column

3) Column operation
ncols = table.ncols #Get the effective number of columns in the table
table.col (colx, start_rowx = 0, end_rowx = None) #Return a list of all cell objects in the column
table.col_slice (colx, start_rowx = 0, end_rowx = None) #return a list of all cell objects in the column
table.col_types (colx, start_rowx = 0, end_rowx = None) #return a data type composed of all cells in the column List
table.col_values ​​(colx, start_rowx = 0, end_rowx = None) #Return a list consisting of the data of all cells in the column

4) Cell operation
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) # Not yet understood

Published 38 original articles · Like 29 · Visits 10,000+

Guess you like

Origin blog.csdn.net/weixin_45270982/article/details/105547904