First, install the XLRD
Address
After downloading, use the pip install .whl
installation as well.
View Help:
>>> Import xlrd>>> Help (XLRD) to the Xlrd:name xlrdpackage CONTENTS biffh book CompDoc Formatt ING Formula Info licences sheet TimeMachine xldate xlsxfunctions count_records (filename, outfile=<idlelib. Pyshell.pseudooutputfile object at 0x0287e730>) dump (filename, outfile=<idlelib. Pyshell.pseudooutputfile object at 0x0287e730>, Unnumbered=false) Open_workbook (Filename=none, logfile=< Idlelib. Pyshell.pseudooutputfile object at 0x0287e730>, verbosity=0, Use_mmap=1, File_contents=none, encoding_override= None, Formatting_info=false, On_demand=false, ragged_rows=false) DATA Fmla_type_array = 4 Fmla_type_cell = 1 FMLA_TYPE_CO ND_FMT = 8 Fmla_type_data_val = Fmla_type_name = 1 fmla_type_shared = 2 Mmap_available = 1 Use_mmap = XL_CELL_BLANK = 6 Xl_cell_boolean = 4 Xl_cell_date = 3 Xl_cell_empty = 0 Xl_cell_error = 5 Xl_cell_number = 2 Xl_cell_text = 1 __VERSION __ = ' 1.0.0 ' Biff_text_from_num = {0: ' (not Biff) ', 20: ' 2.0 ', 21: ' 2.1 ', 30: ' 3 ',... Empty_cell = Empty: ' Error_text_from_code = {0: ' #NULL! ', 7: ' #p/0! ', ' #VALUE! ', at: ... obool = 3 Oerr = 4 Onum = 2 Oref = 1 Orel =-2 OSTRG = 1 Ounk = 0 Okind_dict = {-2: ' Orel ',-1: ' Oref ', 0: ' Ounk ', 1: ' Ostrg ', 2: ' Onum ' ... FILE c:\python34\lib\site-packages\xlrd\__init__.py
The above method can be used to view the XLRD help information, there are some modules in the XLRD package, as well as some member variables, constants, functions.
Second, Python processing Excel table
1. Open Excel Table
Import xlrd# Gets a book object book = Xlrd.open_workbook ("1.xls") # Gets a list of sheet objects sheets = book.sheets () # loops through each sheet, Output the name of this sheet (if it is a new XLS table, it may be Sheet1, Sheet2, Sheet3) for sheet in sheets: print (Sheet.name)
The above help message appears with this function: open_workbook()
open the workbook, which opens the Excel table.
Return is a book object, through the book object we can get a list of sheet, the above program simply to the name of each sheet to lose out.
2. read out the data in the specified cell
Import xlrd# Gets a book object book = Xlrd.open_workbook ("1.xls") # Gets a list of sheet objects sheets = book.sheets () # loops through each sheet, Output the name of this sheet (if it is a new XLS table, it may be Sheet1, Sheet2, Sheet3) for sheet in sheets: print (sheet.cell_value (0, 0))
read out the cells in the data function cell_value(row, col)
, the rows and columns from 0 onwards.
In addition to this, you can:
Sheet.cell (Row, col) # Get Cell object Sheet.cell_type (Row, col) # get cell type
3. Read Date data
If a cell data stored by Excel is a date, it needs to be processed, converted to a datetime
type
From datetime import datetime from XLRD import xldate_as_tuple# gets a book object book = Xlrd.open_workbook ("1.xls") # Gets a sheet object List of sheets = Book.sheets () Timeval = Sheets[0].cell_value (0,0) timestamp = DateTime (*xldate_as_tuple (timestamp, 0)) print (timestamp)
4. Traverse the data of each row
rows = Sheet.get_rows () for row in rows: print (row[0].value) # outputs data for the first column of this row