Detailed Python XLRD package installation and processing Excel table _python

Source: Internet
Author: User
Tags datetime

I. Installation of XLRD

Address

After downloading, use the pip install .whl installation is good.

View Help:

>>> Import xlrd >>> Help (XLRD) to package Xlrd:name xlrd package CONTENTS BIFFH Book Comp Doc formatting Formula info licences sheet TimeMachine xldate xlsx functions count_records (filename, OUTFILE=&L T;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_TYP E_COND_FMT = 8 Fmla_type_data_val = Fmla_type_name = fmla_type_shared = 2 Mmap_available = 1 Use_mmap = 1 XL_C Ell_blank = 6 Xl_cell_boolean = 4 Xl_cell_date = 3 Xl_cell_empty = 0 Xl_cell_error = 5 Xl_cell_number = 2 XL_CELL_TE XT = 1 __version__ = ' 1.0.0 ' Biff_text_from_num = {0: ' (Not BIFF) ' 2.0 ': ' 2.1 ', ' 3 ', ... Empty_cell = empty: ' Error_text_from_code = {0: ' #NULL! ', 7: ' #DIV/0! ', : ' #VALUE! ', 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 methods allow you to view the XLRD Help information, which contains some modules in the XLRD package and some member variables, constants, and functions.

Second, Python processing Excel table

1. Open Excel Table

Import xlrd

# Get a book object book
= Xlrd.open_workbook ("1.xls")

# Get a list of sheet objects
sheets = book.sheets ()

# Iterate through each sheet, outputting this sheet name (if it is a newly created XLS table, it could be Sheet1, Sheet2, Sheet3) for
sheet in sheets: 
 print ( Sheet.name)

This function appears in the Help information above: open_workbook() Open the workbook, which opens the Excel table.

Returns a book object, through which we can get a sheet list, and the above program simply loses each sheet's name.

2, read out the data in the specified cell

Import xlrd

# Get a book object book
= Xlrd.open_workbook ("1.xls")

# Get a list of sheet objects
sheets = book.sheets ()

# Iterate through each sheet, outputting the sheet name (if it is a newly created XLS table, possibly Sheet1, Sheet2, Sheet3) for
sheet in sheets: 
 print ( Sheet.cell_value (0, 0))

Read the data function in the cell cell_value(row, col) , and the rows are from 0.

In addition, you can pass:

Sheet.cell (Row, col) # Get the Cell object 

3. Read Date data

If one of the cell data stored in Excel is a date, it needs to be processed and 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 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 each row of data

rows = Sheet.get_rows () 
for row in rows: 
 print (row[0].value) # output data from the first column of this row

Summarize

The above is the entire content of this article, I hope the content of this article for everyone to learn or use Python can bring some help, if you have questions you can message exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.