Python USES the third party library xlrd to write to the Excel file example
- 2020-05-05 11:26:06
- OfStack
After the last article using xlrd to read Excel, this article will introduce how to write Excel and Excel, we need to use xlwt, xlrd means read xls, xlwt means write xls, just like xlrd, xlrd means read xls, xlwt means write xls, also the current version only supports Excel version 97-03. xlwt download: xlwt 0.7.4
installs xlwt
The installation mode is python setup.py install will be fine, or directly unzip to your project directory.
API introduces
Gets an instance of xls,
xls = ExcelWrite.Workbook()
Add an sheet
sheet = xls.add_sheet("Sheet1")
Write data
to sheet
sheet.write(row_index, col_index, value)
Save to generate xls
xls.save(file_name)
USES xlwt
# -*- 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. The more functions of API require readers to learn from the download package, such as formatting, hyperlinks, formulas and so on.