Install and process the xlrd package in python in an Excel file, pythonxlrd
1. Install xlrd
Address
After the download, usepip install .whl
Installation is good.
View help:
>>> import xlrd>>> help(xlrd)Help on package xlrd:NAME xlrdPACKAGE CONTENTS biffh book compdoc formatting 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_COND_FMT = 8 FMLA_TYPE_DATA_VAL = 16 FMLA_TYPE_NAME = 32 FMLA_TYPE_SHARED = 2 MMAP_AVAILABLE = 1 USE_MMAP = 1 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: '#DIV/0!', 15: '#VALUE!', 23: ... 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
Through the above method, you can view the help information of xlrd, which contains some modules in the xlrd package and some member variables, constants, and functions.
Ii. processing Excel tables in python
1. Open an Excel table
Import xlrd # obtain a Book object book = xlrd. open_workbook ("1.xls") # obtain a list of sheet objects. sheets = book. sheets () # traverse each sheet and output the name of this sheet (if a new xls table is created, it may be sheet1, sheet2, and sheet3) for sheet in sheets: print (sheet. name)
The above help information shows this function:open_workbook()
Open the workbook, which opens the Excel table.
The returned table is a Book object. Through the Book object, we can obtain a list of Sheet. The above program simply loses the name of each sheet.
2. read data from a specified cell.
Import xlrd # obtain a Book object book = xlrd. open_workbook ("1.xls") # obtain a list of sheet objects. sheets = book. sheets () # traverse each sheet and output the name of this sheet (if a new xls table is created, it may be sheet1, sheet2, and sheet3) for sheet in sheets: print (sheet. cell_value (0, 0 ))
Read data functions in Cellscell_value(row, col)
, The number of rows and columns starts from 0.
In addition, you can:
Sheet. cell (row, col) # Get the cell Object sheet. cell_type (row, col) # Get the cell type
3. Read date data
If the data in a cell stored in Excel is a date, you need to process it and convert itdatetime
Type
From datetime import datetime from xlrd import xldate_as_tuple # obtain a Book object book = xlrd. open_workbook ("1.xls") # obtain a list of sheet objects. sheets = book. sheets () timeVal = sheets [0]. cell_value (0, 0) timestamp = datetime (* xldate_as_tuple (timestamp, 0) print (timestamp)
4. Traverse data in each row
Rows = sheet. get_rows () for row in rows: print (row [0]. value) # output data in the first column of this row
Summary
The above is all about this article. I hope this article will help you learn or use python. If you have any questions, please leave a message.