How to read and write Excel files using the xlrd module in python

Source: Internet
Author: User

How to read and write Excel files using the xlrd module in python

This article mainly introduces how to read and write Excel files using the xlrd module in python. It analyzes in detail the installation, usage, and related skills of the xlrd module. For more information, see

1. Install the xlrd Module

Go to the python official website and download http://pypi.python.org/pypi/xlrd.pdf. The python environment has been installed.

II. Introduction

1. Import Module

The Code is as follows:

Import xlrd

2. Open an Excel file to read data

The Code is as follows:

Data = xlrd.open_workbook('excelFile.xls ')

3. Tips

Get a worksheet

The Code is as follows:

Table = data. sheets () [0] # obtain from index order

Table = data. sheet_by_index (0) # obtain from index order

Table = data. sheet_by_name (u'sheet1') # obtain it by name

Get the value of the whole row and the whole column (array)

The Code is as follows:

Table. row_values (I)

Table. col_values (I)

Obtain the number of rows and columns

The Code is as follows:

Nrows = table. nrows

Ncols = table. ncols

Cyclic row List 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

Simple Writing

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 value'

Xf = 0 # extended formatting

Table. put_cell (row, col, ctype, value, xf)

Table. cell (0, 0) # cell value'

Table. cell (0, 0). value # cell value'

Iii. Demo code

The Demo code is actually very simple, that is, reading Excel Data.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

#-*-Coding: UTF-8 -*-

Import xdrlib, sys

Import xlrd

Def open_excel (file = 'file.xls '):

Try:

Data = xlrd. open_workbook (file)

Return data

Except t Exception, e:

Print str (e)

# Retrieving data parameters in an Excel table based on indexes: file: Excel file path colnameindex: the row where the header column is located, so by_index: The table Index

Def excel_table_byindex (file = 'file.xls ', colnameindex = 0, by_index = 0 ):

Data = open_excel (file)

Table = data. sheets () [by_index]

Nrows = table. nrows # number of rows

Ncols = table. ncols # Number of Columns

Colnames = table. row_values (colnameindex) # a row of data

List = []

For rownum in range (1, nrows ):

Row = table. row_values (rownum)

If row:

App = {}

For I in range (len (colnames )):

App [colnames [I] = row [I]

List. append (app)

Return list

# Obtain the data parameters in the Excel table by name: file: Excel file path colnameindex: the row where the header column is located, so 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 # number of rows

Colnames = table. row_values (colnameindex) # a row of data

List = []

For rownum in range (1, nrows ):

Row = table. row_values (rownum)

If row:

App = {}

For I in range (len (colnames )):

App [colnames [I] = row [I]

List. append (app)

Return list

Def main ():

Tables = excel_table_byindex ()

For row in tables:

Print row

Tables = excel_table_byname ()

For row in tables:

Print row

If _ name __= = "_ main __":

Main ()

I hope this article will help you with 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.