Python Operations Excel Table read-write--XLRD module

Source: Internet
Author: User

Reproduced original address: http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html

First, install the XLRD module

Download the HTTP://PYPI.PYTHON.ORG/PYPI/XLRD module installation to the Python website, provided the Python environment is already installed.

Install: Unpack the package, Windows command line mode, go to unzip file directory, execute: Python setup.py install

Second, the use of the introduction

1. Import Module

Import xlrd

2. Open Excel file to read data

data = Xlrd.open_workbook ('excelfile.xls')

3, the use of skills

Get a worksheet

        Table = data.sheets () [0]          #            obtained by index order           # obtained by index order = Data.sheet_by_name (U'Sheet1')# gets by name
get the values (arrays) for the entire row and the entire column
         Table.row_values (i)          table.col_values (i)
get the number of rows and columns 
        nrows = table.nrows         = Table.ncols
cyclic row and column table data
 for inch Range (nrows):       Print table.row_values (i)
Cell Cell
    CELL_A1 = Table.cell (0,0). Value     = Table.cell (2,3). Value
using row and column indexes
    CELL_A1 = table.row (0) [0].value     = table.col (1) [0].value
a simple write
row == 0  #  type 0 empty,1 string, 2 number, 3 date, 4 Boolean, 5 error' unit Lattice values '#  extended format Table.put_cell (Row, col, CType, value, XF) Table.cell (0,0)   # the value of the cell '  # value of cell '

Third, demo code

The demo code is simple enough to read the Excel data.

#-*-coding:utf-8-*-Importxdrlib, sysImportxlrddefOpen_excel (file='File.xls'):    Try: Data=xlrd.open_workbook (file)returnDataexceptexception,e:PrintStr (e)#get data parameters in Excel table According to index: File:excel file path Colnameindex: Table header column name is the row, so by_index: Index of TabledefExcel_table_byindex (file='File.xls', colnameindex=0,by_index=0): Data=open_excel (file) Table=data.sheets () [By_index] nrows= Table.nrows#Number of rowsNcols = Table.ncols#Number of columnsColnames = Table.row_values (Colnameindex)#a row of dataList =[]     forRowNuminchRange (1, nrows): Row=table.row_values (rownum)ifRow:app= {}              forIinchRange (len (colnames)): App[colnames[i]]=Row[i] List.append (APP)returnList#get data parameters in Excel table by name: File:excel file path Colnameindex: The header column name is the row, so the By_name:sheet1 namedefExcel_table_byname (file='File.xls', Colnameindex=0,by_name=u'Sheet1'): Data=open_excel (file) Table=data.sheet_by_name (by_name) nrows= Table.nrows#Number of rowsColnames = Table.row_values (Colnameindex)#a row of dataList =[]     forRowNuminchRange (1, nrows): Row=table.row_values (rownum)ifRow:app= {}              forIinchRange (len (colnames)): App[colnames[i]]=Row[i] List.append (APP)returnListdefMain (): Tables=Excel_table_byindex () forRowinchtables:PrintRow Tables=Excel_table_byname () forRowinchtables:PrintRowif __name__=="__main__": Main ()

Python Operations Excel Table read-write--XLRD module

Related Article

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.