Python operations Excel Read-write-using XLRD

Source: Internet
Author: User

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.

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] #通过索引顺序获取 table = data.sheet_by_index (0) #通过索引顺序获取

Table = data.sheet_by_name (U ' Sheet1 ') #通过名称获取 get the values (arrays) for the entire row and the entire columnTable.row_values (i) table.col_values (i) get the number of rows and columnsnrows = table.nrows Ncols = Table.ncols cyclic row and column table dataFor I in Range (nrows): Print table.row_values (i) Cell CellCELL_A1 = Table.cell (0,0). Value cell_c4 = Table.cell (2,3). Value using row and column indexesCELL_A1 = Table.row (0) [0].value cell_a2 = Table.col (1) [0].value a simple writerow = 0 col = 0 # type 0 empty,1 string, 2 number, 3 date, 4 Boolean, 5 Errorctype = 1 value = ' cell values ' XF = 0 # Extended formatted table . Put_cell (Row, col, CType, value, XF) Table.cell (0,0) #单元格的值 ' Table.cell (0,0). Value #单元格的值 '

Third, demo code

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

 1 #-*-Coding:utf-8-*-  2 import xdrlib, sys 3 import XLRD 4 def open_excel (file= ' File.xls '): 5 try:6 data = Xlrd.open_workbook (file) 7 return data 8 except Exception,e:9 print str (e) #根据索引获取Excel Data parameters in table: File:excel file path Colnameindex: Table header column name is located in the row, so by_index: Table index  11 def excel_table_byindex (file= ' File.xls ', colnameindex=0,by_index=0): data = open_excel (file) Table = Data.sheets () [By_index]14 nrows = Table.nrows #行数  15 ncols = table.ncols #列数  16 colnames = table.row_values (colnameindex) #某一行数据  17 list =[  ]18 for rownum in range (1,nrows): row = Table.row_values (rownum) if row:22 app = {}23 for I in range (len (colnames)): [app[colnames[i]] = Row[i] List.append ( APP)-list27 #根据名称获取Excel表格中的数据 parameter: File:excel file path Colnameindex: The header column name is the row, so By_name:sheet1 name  29 def exc El_table_byname (file= ' File.xls ',Colnameindex=0,by_name=u ' Sheet1 '): data = open_excel (file) to table = Data.sheet_by_name (by_name) nrows = t Able.nrows #行数  33 colnames = table.row_values (colnameindex) #某一行数据  34 list =[]35 for rownum in RA  Nge (1,nrows): $ row = table.row_values (rownum) PNS if row:38 app = {}39 for i in Range (len (colnames)): app[colnames[i]] = row[i]41 list.append (APP) return list43 44 def main (): tables = Excel_table_byindex (), for row in tables:47 print row48 tables = excel_table_by Name () for row in tables:51 print row52 if __name__== "__main__": Si Main ()

Python operations Excel Read-write-using xlrd

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.