import xlwt
import xlrd
wb = xlwt.Workbook()
sheet = wb.add_sheet('颜色列表')
dic = xlwt.Style.colour_map
#加粗字体
#创建一个样式
style1 = xlwt.XFStyle()
#创建字体对象
font1 = xlwt.Font()
#将字体对象关联到样式中
style1.font = font1
#加粗
font1.bold = True #是否加粗
font1.colour_index = 8 #设置字体颜色
# print(dic)
sheet.write(0, 0,'颜色',style1)
sheet.write(0, 1,'颜色单词',style1)
sheet.write(0,2,'颜色号',style1)
#颜色列表
row = 1
for _ in dic:
#创建样式对象
style1 = xlwt.XFStyle()
# 水平居中
# 创建居中对象
alignment1 = xlwt.Alignment()
# 将居中对象关联到样式
style1.alignment = alignment1
# 居中模式
alignment1.vert = xlwt.Alignment.VERT_CENTER
alignment1.horz = xlwt.Alignment.HORZ_CENTER
sheet.write(row, 1, _,style1)
sheet.write(row, 2,dic[_],style1)
#创建填充对象
pattern1 = xlwt.Pattern()
# 将填充对象关联到样式中
style1.pattern = pattern1
# =========填充样式============
pattern1.pattern = xlwt.Pattern.SOLID_PATTERN # 设置填充模式,整个单元格一起填充
pattern1.pattern_fore_colour = dic[_] # 设置填充颜色
# 写入数据时添加样式
sheet.write(row, 0, '',style1)
#==========设置行高===========
# 设置行高
# 允许设置某一行的高度
sheet.row(row).height_mismatch = True
# 设置某一行的高度
sheet.row(row).height = 20*40
row += 1
# 1.设置列宽度
sheet.col(0).width = 20 * 256
sheet.col(1).width = 20 * 256
sheet.col(2).width = 20 * 256
wb.save('files/data5.xls')
day08-作业
最新推荐文章于 2024-07-30 21:09:58 发布