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.