1. What is the XLRD module?
2. Why use the XLRD module?
3. How do I use the XLRD module?
1. What is the XLRD module?
The Python operation Excel mainly uses the XLRD and XLWT the two libraries, namely XLRD is the read EXCEL,XLWT is writes the Excel library.
Let's start today. XLRD module:
First, install the XLRD module
? Download the HTTP://PYPI.PYTHON.ORG/PYPI/XLRD module installation to the Python website, provided the Python environment is already installed.
? or in the CMD window pip install xlrd
Second, the use of the introduction
? 0. Empty (empty), 1 string (text), 2 number, 3 date, 4 Boolean, 5 error, 6 blank (blank table)
Import xlrd
data = xlrd.open_workbook (filename)# file name and path, if the path or file name has Chinese to the front plus an R apprentice native character.
? The most important method in Excel is the operation of book and sheet
1) Get a worksheet in book
Table = data.sheets () [0] # Get # by index order get table = Data.sheet_by_name (sheet_name)# Get # By name # returns the name of all worksheets in book # Check if a sheet is finished importing
Such as:
2) operation of the line
nrows = table.nrows #< Span style= "COLOR: #008000" > Gets the number of valid rows in the sheet table.row (ROWX) # Returns a list of all the cell objects in the row Table.row_slice (ROWX) # Returns a list of all the cell objects in the column table.row_types (Rowx, start_colx=0, End_colx=none) # Returns a list of data types for all cells in the row table.row_values (Rowx, start_colx=0, end_colx= None) # Returns a list of the data that is made up of all the cells in the row table.row_len (rowx) # Returns the valid cell length for this column
3) operation of the column (Colnum)
Ncols = Table.ncols # Gets the number of valid columns for the list table.col (Colx, start_rowx=0, End_rowx=none) # Returns a list of all the cell objects in the column # Returns a list of all the cell objects in the column # Returns a list of the data types of all cells in the column # Returns a list of the data that is made up of all the cells in the column
Such as:
4) operation of the cell
Table.cell (ROWX,COLX) # Returns the Cell object table.cell_type (ROWX,COLX) # Returns the data type in the cell # Return the data in a cell # not yet understand
Cell: A cell is a cross-section of a row and column in a table, which is the smallest unit that makes up a table and can be split or merged. The input and modification of a single data is done in a cell
Such as:
Note: Note the scope issue, after obtaining the sheet, after getting to this sheet value, in the row and column as well as the operation of the cell.
? python resolves the open () function, Xlrd.open_workbook () function file name contains Chinese, sheet name contains the problem of Chinese error
problem Phenomenon:
? 1, open the file using the open () function, the Xlrd.open_workbook () function, if the file name contains Chinese, it will be an error to find the files or directories.
2, if the acquisition of sheet when the Chinese, will also error.
# Opening file = open (filename,'RB')# Open Excel File workbook = xlrd.open_workbook (filename) # Get sheetsheet = Workbook.sheet_by_name (sheetname)
Solution:
? transcode the parameters. Such as:
filename = Filename.decode ('utf-8')
The Unicode function was also tried, however, an error occurred while running in ride, so it was deprecated.
filename = Unicode (filename,'utf-8')
2. Why use the XLRD module?
? Data maintenance is a core in UI Automation or interface automation, so this module is very useful.
3. How do I use the XLRD module?
Python xlrd XLWT