文章目录
#创建并保存表格
#newexcel = xlwt.Workbook(encoding= 'utf-8') //创建新的excel,说明字符编码为utf8
#newsheet = newexcel.add_sheet('newsheet1') //创建新的sheet表
#newsheet.write(0,0, "text1") //在表格写入内容
#newsheet.write(1,1, "text2") //在表格写入内容
#newexcel.save('test.xls') //保存
#设置字体格式
#newexcel = xlwt.Workbook(encoding= 'utf-8') //创建新的excel,说明字符编码为utf8
#newsheet2 = newexcel.add_sheet('newsheet2') //创建新的sheet表
#style = xlwt.XFStyle() //初始化样式
#font = xlwt.Font() //为样式创建字体
#font.name = '宋体' //设置字体
#font.bold = True //加粗
#font.underline = True //下划线
#font.italic = False //斜体
#style.font = font //设置样式
#newsheet2.write(0,0, "ceshineirong1") //往表格写入内容
#newsheet2.write(2,1, "ceshineirong2",style) //往表格写入内容,标注style为设置样式(使用样式)
#newexcel.save("test.xls") //保存
#newsheet2.col(0).width = 256*20 //设置列宽,要放在表内容前
#设置行高
#--往表格写入内容后--#
#style = xlwt.easyxf('font:height 360;') //18pt,类型小初的字号
#row = newsheet2.row(0)
#row.set_style(style)
#newexcel.save("test.xls") //保存
#合并单元格
#--往表格写入内容后--#
#newsheet2.write_merge(1, 2, 0, 3, '合并单元格') //合并单元格,可在添加的数据后面加逗号设定格式,如:(1, 2, 0, 3, '合并单元格', style1)
#newexcel.save("test.xls") //保存
#单元格添加边框
#--往表格写入内容后--#
#borders = xlwt.Borders()
#borders.left = xlwt.Borders.THIN //左边框,THIN为实线
#borders.right = xlwt.Borders.THIN //右边框
#borders.top = xlwt.Borders.THIN //上边框
#borders.bottom = xlwt.Borders.THIN //下边框
#borders.left_colour = 0x40 //左边框颜色
#borders.right_colour = 0x40 //右边框颜色
#borders.top_colour = 0x40 //上边框颜色
#borders.bottom_colour = 0x40 //下边框颜色
#style = xlwt.XFStyle() //设置样式
#style.borders = borders //向样式添加边框
#--保存--#
#################
#DASHED - - 虚线#
#NO_LINE - -没有#
#THIN - - - 实线#
#################
#设置单元格背景颜色
#pattern = xlwt.Pattern() //创建样式
#pattern.pattern = xlwt.Pattern.SOLID_PATTERN //可以是NO_PATTERN, SOLID_PATTERN, or 0x00 到 0x12
#pattern.pattern_fore_colour = 5 //设置背景颜色
#style1 = xlwt.XFStyle()
#style1.pattern = pattern
####################################################################################
#0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, #
#6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, #
#19 = Dark Yellow ,20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, #
####################################################################################
#设置单元格对齐
#style = xlwt.XFStyle() //设置样式
#al = xlwt.Alignment()
#al.horz = 0x02 //设置水平居中
#al.vert = 0x01 //设置垂直居中
#style.alignment = al
#newsheet2.write(6,6, "ceshineirong1") //数据写入
#newsheet2.write(7,7, "ceshineirong2",style) //数据写入,并写入样式
#--保存--#
#################################################
#VERT_TOP = 0x00 - - - - 上端对齐 #
#VERT_CENTER = 0x01 - - - 居中对齐(垂直方向上) #
#VERT_BOTTOM = 0x02 - - - 低端对齐 #
#HORZ_LEFT = 0x01 - - - - 左端对齐 #
#HORZ_CENTER = 0x02 - - - 居中对齐(水平方向上) #
#HORZ_RIGHT = 0x03 - - - 右端对齐 #
#################################################
暑期编程PK赛
得CSDN机械键盘等精美礼品!