1 #-*-coding:utf-8-*-2 """3 Created on Thu 13:53:10 20184 5 @author: Frank6 """7 8 Importxlrd #XLRD is a library for reading data and formatting information from Excel files, whether they is. xls or. xlsx files. 9 Tendata = Xlrd.open_workbook ('Address Book. xlsx') #打开 Excel file One Print(Type (data)) ATable =data.sheets () [0] - Print(Type (table)) -nrows = Table.nrows#Number of rows the Print("number of rows: {}". Format (nrows)) -Ncols = Table.ncols#Number of columns - Print("Number of columns: {}". Format (ncols)) - forIinchRange (0,nrows): +Rowvalues = Table.row_values (i)#a row of data - #print (Type (rowvalues)) + #print (rowvalues) A Print("name:{}<--> phone: {}". Format (Rowvalues[0], rowvalues[1]))
xlrd.
open_workbook
(filename=none, logfile=<_io. Textiowrapper name= ' <stdout> ' mode= ' W ' encoding= ' UTF-8 ' >, verbosity=0, use_mmap=1, file_contents=none, encoding_override=none, formatting_info=false, on_demand= False, ragged_rows=false)
Open an Excel file.
Here is a simple introduction to the basic parameters, later useful to add.
Parameters:
filename , haven path to the spreadsheet file to is opened.
Specify the path to open the file
logfile –an Open file to which messages and diagnostics is written
Returns:
An instance of the Book
class.
Book.sheets ()
RETURN:A List of all sheets in the book. Returns all the tables in Excel and saves them in the list.
All sheets not already loaded would be loaded.
xlrd.sheet.
class Sheet
(book, position, name, number)
Contains the data for one worksheet.
In the cell access functions, was rowx
a row index, counting from zero, and was colx
a column index, counting from zero. N Egative values for Row/column indexes and slice positions is supported in the expected fashion.
Note:you don ' t instantiate this class yourself. You access Sheet
objects via the object that is returned when you Book
called xlrd.open_workbook()
.
nrows
= 0
Number of rows in sheet. A row index is in range(thesheet.nrows)
.
ncols
= 0
Nominal number of columns in sheet. It's one more than the Maximum column index found, ignoring trailing empty cells. See also the ragged_rows
parameter to open_workbook()
and row_len()
.
row_values
(rowx, start_colx=0, end_colx=none)
Returns a slice of the values of the cells in the given row. The function returns a list that contains all the contents of cells in this row by default.
Python read Excel file