Details the installation and processing of the Excel form for the xlrd package in python

  • 2020-05-19 05:06:45
  • OfStack

1. Install xlrd

address

After downloading, use pip install .whl Just install.

See help:


>>> import xlrd
>>> help(xlrd)
Help on package xlrd:

NAME 
 xlrd

PACKAGE CONTENTS 
 biffh
 book
 compdoc
 formatting
 formula
 info
 licences
 sheet
 timemachine
 xldate
 xlsx

FUNCTIONS 
 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

You can view the xlrd help information in the above method, which includes some modules and some member variables, constants and functions in the xlrd package.

2. python processes Excel forms

1. Open the Excel table


import xlrd

#  To obtain 1 a Book object 
book = xlrd.open_workbook("1.xls")

#  To obtain 1 a sheet List of objects 
sheets = book.sheets()

#  Traverse every 1 a sheet , output this sheet Name (if new) 1 a xls It could be sheet1 , sheet2 , sheet3 ) 
for sheet in sheets: 
 print(sheet.name)

The help above appears in this function: open_workbook() , open the workbook, which opens the Excel table.

What is returned is an Book object, through which we can obtain a list of Sheet. The program above simply typed out the name of each sheet.

2. Read the data in the specified cell


import xlrd

#  To obtain 1 a Book object 
book = xlrd.open_workbook("1.xls")

#  To obtain 1 a sheet List of objects 
sheets = book.sheets()

#  Traverse every 1 a sheet , output this sheet Name (if new) 1 a xls It could be sheet1 , sheet2 , sheet3 ) 
for sheet in sheets: 
 print(sheet.cell_value(0, 0))

Read the data function inside the cell cell_value(row, col) The rows and columns all start from 0.

In addition, it can be done by:


sheet.cell(row, col) #  Gets the cell object  
sheet.cell_type(row, col) #  Gets the cell type  

3. Read date data

If the data of one cell stored by Excel is a date, it needs to be processed 1 to convert to datetime type


from datetime import datetime 
from xlrd import xldate_as_tuple

#  To obtain 1 a Book object 
book = xlrd.open_workbook("1.xls")

#  To obtain 1 a sheet List of objects 
sheets = book.sheets()

timeVal = sheets[0].cell_value(0,0)

timestamp = datetime(*xldate_as_tuple(timestamp, 0))

print(timestamp)

4. Walk through the data in each row


rows = sheet.get_rows() 
for row in rows: 
 print(row[0].value) #  Output line order 1 Columns of data 

conclusion

The above is the whole content of this article, I hope the content of this article can help you to learn or use python, if you have any questions, you can leave a message to communicate.


Related articles: