New excel file --xlwt library - otherwise variable 01

Reprinted: https://www.jianshu.com/p/fc97dd7e822c

Set the cell alignment

= xlwt.Workbook Workbook (encoding = ' UTF-. 8 ' ) 
Worksheet = workbook.add_sheet ( ' Sheet1 ' ) 
Alignment = xlwt.Alignment ()
 # centered horizontally 
alignment.horz = xlwt.Alignment.HORZ_CENTER
 # vertical centering 
alignment.vert = xlwt.Alignment.VERT_CENTER 
style = xlwt.XFStyle () 
style.alignment = Alignment
 # set the cell width 
worksheet.col (0) = 6666 .width # set the cell height 
worksheet.row (0) .height_mismatch = True 
Worksheet. row (0) .height
 
= 1000
 
worksheet.write(0, 0, 'hello world', style)
workbook.save('center.xls')

 

 

 

 

 

Set the border style unit

workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('sheet1')
border = xlwt.Borders()
# DASHED虚线
# NO_LINE没有
# THIN实线
border.left = xlwt.Borders.THIN
# 设置颜色
border.left_coloure = 0x40b
border.right = xlwt.Borders.THIN
 
border.right_colour = 0x40b
border.top = xlwt.Borders.THIN
 
border.top_colour = 0x40b
border.bottom = xlwt.Borders.THIN
 
border.bottom_colour = 0x40b
style = xlwt.XFStyle()

 

 

 

 

Cell borders

workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('sheet1')
 
border = xlwt.Borders()
# DASHED虚线
# NO_LINE没有
# THIN实线
border.left = xlwt.Borders.THIN
border.right = xlwt.Borders.THIN
border.top = xlwt.Borders.THIN
border.bottom = xlwt.Borders.THIN
 
style = xlwt.XFStyle()
style.borders = border
worksheet.write(1, 1, 'love', style)
 
workbook.save('dashed.xls')

 

 

 

 

 

Setting the background color of the cell

workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('sheet1')
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
 
# 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...
 
pattern.pattern_fore_colour = 3
style = xlwt.XFStyle()
style.pattern = pattern
worksheet.write(1, 1, 'shit', style)
workbook.save('shit.xls')

 

 

 

 

 

Set the font color

workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('sheet1')
 
font = xlwt.Font()
# 设置字体为红色
font.colour_index=xlwt.Style.colour_map['red']
 
style = xlwt.XFStyle()
 
style.font = font
 
worksheet.write(0, 1, 'world', style)
workbook.save('students.xls')

 

Guess you like

Origin www.cnblogs.com/xiaobaibailongma/p/12370087.html