Python writes Excel files and requires XLWT libraries. : https://pypi.python.org/pypi/xlwt/1.1.2
After download, modify the extension to RAR and install after decompression:
Once the installation is successful, it can be referenced. The following code:
#-*-Coding:utf-8-*-
Import XLWT
"' Write content to Excel cells, write one line at a time sheet: page signature; row: Row table of Contents; RowIndex: Row index" '
def writesheetrow (Sheet,rowvaluelist,rowindex):
i = 0
For svalue in Rowvaluelist:
strvalue = Unicode (str (svalue), ' Utf-8 ')
Sheet.write (Rowindex,i,strvalue)
i = i + 1
"Write Excel File"
def save_excel (strfile):
Excelfile = Unicode (strfile, "UTF8")
WBK = XLWT. Workbook ()
Sheet = wbk.add_sheet (' Sheet1 ')
Headlist = [' Heading 1 ', ' Heading 2 ', ' Heading 3 ', ' Title 4 ']
RowIndex = 0
Writesheetrow (Sheet,headlist,rowindex)
For I in Xrange (1,11):
RowIndex = RowIndex + 1
ValueList = []
For j in Xrange (1,5):
Valuelist.append (J*i)
Writesheetrow (Sheet,valuelist,rowindex)
Wbk.save (Excelfile)
#测试
Save_excel ("D:\\test.xlsx")
The results are as follows:
Python xlwt writing Excel files