简介:
xlwt是Python中往excel中写入数据的模块1. 创建Book工作簿(即excel工作簿)
. 1 Import xlwt 2 workbook = xlwt.Workbook (encoding = ' UTF-. 8 ' ) . 3 # Create a workbook and encoded form provided
2. 添加sheet工作表
1 worksheet = workbook.add_sheet('My Worksheet') 2 # 创建一个worksheet
3. 向工作表中添加数据并保存
. 1 worksheet.write (1,0, label = ' the this Test IS ' ) 2 # parameters corresponding to the row, column, value . 3 . 4 workbook.save ( ' Excel_test.xls ' ) . 5 # save
4. 格式设置
4.1 字体设置
. 1 Import xlwt 2 . 3 Workbook = xlwt.Workbook (encoding = ' ASCII ' ) . 4 . 5 Worksheet = workbook.add_sheet ( ' My Worksheet ' ) . 6 # Add the worksheet named My Worksheet . 7 . 8 style = xlwt.XFStyle () . 9 # initialization pattern 10 . 11 font = xlwt.Font () 12 is # Create font style 13 is 14 font.name = ' Times New Roman ' 15 16 Font.Bold = True . 17 # Black body 18 is . 19 font.underline = True 20 is # underlined 21 is 22 is Font.ITALIC = True 23 is # italics 24 25 style.font = font 26 is # set the style 27 28 worksheet.write (0, 0, ' Unformatted value ' ) 29 # writing styles without 30 31 is worksheet.write (. 1, 0, ' the Formatted value ' , style) 32 # styled writing 33 is 34 is workbook.save (' Formatting.xls ' ) 35 # save file
4.2 set the cell width and height:
. 1 Import xlwt 2 . 3 Workbook = xlwt.Workbook () . 4 Worksheet = workbook.add_sheet ( ' My Sheet ' ) . 5 worksheet.write (0, 0, ' My the Contents the Cell ' ) . 6 . 7 . 8 worksheet.col (0) .width = 3333 . 9 # set the cell width 10 worksheet.col (0) = 3333 .height . 11 # set the cell width 12 is 13 is workbook.save ( ' cell_width.xls ' )