import xlrd import xlwt from xlutils.copy import copy import time import datetime def setStyle(name,height,color,bold,horz,bcolor): # 初始化样式 style = xlwt.XFStyle() # 为样式创建字体 font = xlwt.Font() font.name = name # 字体类型:比如宋体、仿宋也可以是汉仪瘦金书繁 font.height = height*20 #字体大小 font.colour = color # 设置字体颜色 # 字体大小 font.bold = bold style.font = font # 定义格式 # 为样式创建格式 align = xlwt.Alignment() #创建对齐方式 align.horz = horz #1-->左对齐,2-->居中,3-->右对齐 style.alignment = align # 为样式创建背景颜色 pattern = xlwt.Pattern() pattern.pattern = xlwt.Pattern.SOLID_PATTERN pattern.pattern_fore_colour = bcolor style.pattern = pattern return style rb = xlrd.open_workbook('##########.xls', formatting_info=True) wb = copy(rb) sheet = wb.get_sheet(0) # sheet.write(i,j,...) 写入第i行,j列 for i in range(0,730): date_i=(datetime.datetime.now()+datetime.timedelta(days=i)).strftime("%Y年%m月%d日") date_j=(datetime.datetime.now()+datetime.timedelta(days=i+1)).strftime("%Y年%m月%d日") sheet.write(0,0,date_i+'XXXX情况报送表',setStyle('方正小标简宋',14,'black',False,2,1)) sheet.write(1,0,date_i+'XXXX监测情况',setStyle('宋体', 9, 'black',True,2,22)) sheet.write(44,0,date_i+'XXXX情况',setStyle('宋体', 9, 'black',True,2,22)) sheet.write(53,5,'报送日期:'+date_j,setStyle('宋体', 9, 'black',False,2,1)) wb.save('./结果/'+date_i+'.xls')
xlwt颜色与代码对照表(字体和背景色通用)