您的位置:首页 > 其它

xlwt模块使用

2016-07-27 15:22 381 查看
#-*-coding:utf-8-*-

import xlwt

#新建一个excel文件
wbk = xlwt.Workbook(encoding='utf-8', style_compression=0)

#新建一个sheet
sheet = wbk.add_sheet('警告', cell_overwrite_ok=True)  ##第二参数用于确认同一个cell单元是否可以重设值。

#写入数据table.write(行,列,value)
sheet.write(1,0,'some text')
sheet.write(0,0,'标题')   ##重新设置,需要cell_overwrite_ok=True

#初始化样式
style = xlwt.XFStyle()

pattern = xlwt.Pattern() # Create the Pattern
pattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
pattern.pattern_fore_colour = 5 # May be: 8 through 63. 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 , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on...

#设置字体肩宽
#格式设置
#设置单元框线条:0无,1细实线条,2粗实线,3疏粗虚线,4密细虚线条,5加粗实线
borders= xlwt.Borders()
borders.left= 0
borders.right= 2
borders.top= 0
borders.bottom= 2
#单元框颜色
borders.bottom_colour=0x3A

#为样式创建字体
font = xlwt.Font()
#设置字体加粗
font.bold= True
#设置字体
font.name = 'Times New Roman'
#设置字体的大小i*20
font.height  = 11*20
#字体颜色
font.colour_index= 0
#设置下划线
#font.underline = True
#字体斜体
#font.italic = True

#设置单元格背景色
style = xlwt.easyxf('pattern: pattern solid, fore_colour yellow;')
#为样式设置字体
style.font = font
style.borders = borders
style.pattern = pattern

# 使用样式
sheet.write(0, 1, '内容', style)

#保存文件
wbk.save('new.xls')    ##该文件名必须存在
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  编码 excel