How Python uses XLRD modules to read and write Excel files

Source: Internet
Author: User

This article mainly introduces Python using XLRD module to read and write Excel files, more detailed analysis of the XLRD module installation, use and operation of Excel files related skills, the need for friends can refer to the

First, install XLRD module

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

Second, the use of the introduction

1. Import Module

The code is as follows:

Import xlrd

2, open the Excel file read data

The code is as follows:

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

3, the use of skills

Get a worksheet

The code is as follows:

Table = data.sheets () [0] #通过索引顺序获取

Table = Data.sheet_by_index (0) #通过索引顺序获取

Table = data.sheet_by_name (U ' Sheet1 ') #通过名称获取

Gets the value of an entire row and column (array)

The code is as follows:

Table.row_values (i)

Table.col_values (i)

Get number of rows and columns

The code is as follows:

nrows = Table.nrows

Ncols = Table.ncols

Looping row and column table data

The code is as follows:

For I in Range (nrows):

Print Table.row_values (i)

Cell

The code is as follows:

CELL_A1 = Table.cell (0,0). Value

CELL_C4 = Table.cell (2,3). Value

Use row and column indexes

The code is as follows:

CELL_A1 = Table.row (0) [0].value

CELL_A2 = Table.col (1) [0].value

A simple write

The code is as follows:

Row = 0

Col = 0

# type 0 empty,1 string, 2 number, 3 date, 4 Boolean, 5 error

CType = 1 value = ' cell values '

XF = 0 # Extended format

Table.put_cell (Row, col, CType, value, XF)

Table.cell (0,0) #单元格的值 '

Table.cell (0,0). Value #单元格的值 '

Three, demo code

The demo code is actually very simple to read Excel data.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The #-*-Coding:utf-8-*-import xdrlib, sys import xlrd def open_excel (file= ' File.xls '): Try:data = Xlrd.open_workbook ( File return data except Exception,e:print str (e) #根据索引获取Excel表格中的数据 parameter: File:excel file path colnameindex: Header column name is the row so, By_ Index: Table indexed def excel_table_byindex (file= ' File.xls ', colnameindex=0,by_index=0): data = open_excel (file) Table = Data.sheets () [By_index] nrows = table.nrows #行数 ncols = table.ncols #列数 colnames = table.row_values (colnameindex) #某一行数据 l Ist =[] for rownum in range (1,nrows): row = Table.row_values (rownum) If Row:app = {} to I in range (len (colnames)): App[c Olnames[i]] = Row[i] List.append (APP) return list #根据名称获取Excel表格中的数据 parameters: File:excel file path Colnameindex: Table header column name is the row, By_ Name:sheet1 name def excel_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 #行数 colnames = table.row_values (colnameindex) #某一行数据 list =[] for rownum in range (1,nrows): row = Table.row_va Lues (rownum) If Row:app = {} for I in range (len (colnames)): App[colnames[i] [row[i] List.append (APP) return list def ma In (): tables = Excel_table_byindex () to row in tables:print row tables = Excel_table_byname () to row in Tables:print R ow if __name__== "__main__": Main ()

I hope this article will help you with your Python programming.

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.