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

Source: Internet
Author: User
This article mainly introduces how to read and write Excel files using the xlrd module in python. It analyzes in detail how to install and use the xlrd module and how to operate Excel files, for more information about how to read and write Excel files using the xlrd module in python, see the following example. Share it with you for your reference. The details are as follows:

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.

#-*-Coding: UTF-8-*-import xdrlib, sys import xlrd def open_excel (file = 'file.xls '): try: data = xlrd. open_workbook (file) return data failed t Exception, e: print str (e) # obtain the data parameter in the Excel table based on the index: file: Excel file path colnameindex: the row where the header column name is located, by_index: 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 # columns colnames = table. row_values (colnameindex) # 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 parameter in the Excel table by name: file: Excel file path colnameindex: the row where the header column name 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) # 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.