Following the previous article using XLRD to read Excel, this article is about how to write Excel, write Excel we need to use the Third-party library XLWT, and XLRD, Xlrd said Read XLS,XLWT represents write xls, Also, the current version supports only version 97-03 of Excel. XLWT Download: XLWT 0.7.4
Install XLWT
The way you install it is Python setup.py install, or you can extract it directly into your engineering directory.
API Introduction
Get an XLS instance
Copy Code code as follows:
xls = Excelwrite.workbook ()
Add a sheet
Copy Code code as follows:
Sheet = Xls.add_sheet ("Sheet1")
Writing data to Sheet
Copy Code code as follows:
Sheet.write (Row_index, Col_index, value)
Save Build XLS
Copy Code code as follows:
using XLWT
Copy Code code 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", "Mans"], ["Age", ","], ["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 simply the introduction of how to use the XLWT, the more versatile API requires the reader to learn from the download package, such as formatting, hyperlinks, formulas and so on.