1、基本使用
1.1、创建工作薄
1.2、添加工作表
1.3、向工作表中写入数据
write()方法中的三个参数分别是=(行,列,添加的内容),上面我所添加的就是表头。
1.4、存入本地
2、常用函数
接下来所有操作都针对以上工作薄进行
xw = xlwt.Workbook() # ps:需要注意的点就是通过此方法生成的工作薄,默认的字符编码是ascii,如果有特殊需求,需要自己修改
cla = xw.add_sheet('test')
cla.write(0, 0, 'name') cla.write(0, 1, 'class') cla.write(0, 2, 'cid') cla.write(0, 3, 'gender')
write()方法中的三个参数分别是=(行,列,添加的内容),上面我所添加的就是表头。
xw.save('test.xls')
接下来所有操作都针对以上工作薄进行
sheet = xw.add_sheet('x_test', cell_overwrite_ok=False) # 新建一个sheet,默认参数是不允许覆盖
tall_style = xlwt.easyxf("font:height 720;") sheet.row(0).set_style(tall_style) # ps:row()方法中括号内填写的是行索引
sheet.col(0) # 返回列对象,可以针对它做一系列操作 sheet.col(0).get_width() # 获取列的宽度,默认宽度是2962 sheet.col(0).set_style(tall_style) # 设置列的style sheet.col(0).set_width(2962) # 设置列宽
sheet.write( r, c, label='', style=<xlwt.Style.XFStyle object>) # 向单元格写入数据