Example of using a third-party library xlrd in Python to write an Excel file

Source: Internet
Author: User
This article describes how to use a third-party library xlrd in Python to write an Excel file. This article describes how to install xlwt, introduce the API, and use xlwt to write an Excel file instance, if you need it, refer to the next article using xlrd to read Excel. This article introduces how to write Excel and how to write Excel. we need to use a third-party library xlwt to write Excel, like xlrd, xlrd indicates read xls, xlwt indicates write xls, and the current version only supports Excel of 97-03. Xlwt Download: xlwt 0.7.4

Install xlwt

The installation method is the same as python setup. py install, or directly decompress it to your project directory.

API Introduction

Get an xls instance

The code is as follows:


Xls = ExcelWrite. Workbook ()


Add a sheet

The code is as follows:


Sheet = xls. add_sheet ("Sheet1 ")


Write data to sheet

The code is as follows:


Sheet. write (row_index, col_index, value)


Save and generate xls

The code is as follows:


Xls. save (file_name)


Use xlwt

The code is as follows:


#-*-Coding: UTF-8 -*-
'''
Created on 2012-12-14

@ Author: walfred
@ Module: XLRDPkg. write
@ Description:
'''

Import xlwt as ExcelWrite

Def writeXLS (file_name ):
Value = [["name", "jim", "hmm", "lilei"], ["sex", "man", "woman", "man"], ["age", 19, 24, 24], ["country", "USA", "CHN", "CHN"]
Xls = ExcelWrite. Workbook ()
Sheet = xls. add_sheet ("Sheet1 ")

For I in range (0, 4 ):
For j in range (0, len (value )):
Sheet. write (j, I, value [I] [j])

Xls. save (file_name)

If _ name _ = "_ main __":
WriteXLS ("./test_write.xls ");


Of course, this is just a brief introduction to how to use xlwt. more functions of APIs require readers to learn from the downloaded package, such as setting formats, hyperlinks, and formulas.

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.