查了一些资料发现是英文版本的
很尴尬,经过看源码,和几个错误 ,现记录下来
一:下载包
pip install xlwt
二:定义样式
def set_style(name, height, bold=False):
alignment = xlwt.Alignment() # Create Alignment
alignment.horz = xlwt.Alignment.HORZ_CENTER # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED 居中
alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED,
style = xlwt.XFStyle() # 初始化样式 font = xlwt.Font() # 为样式创建字体
font.name = name # 'Times New Roman' 字体名字
font.bold = bold
font.color_index =
font.height = height #加粗 style.font = font
style.alignment = alignment # Add Alignment to Style
# style.borders = borders return style
三:写入execl 中
他这个写入方式 第一个参数是行,第二个参数是列,第三个参数是你要写的东西,第四个参数是样式
def write_excel():
f = xlwt.Workbook() # 创建工作簿
sheet1 = f.add_sheet(u'sheet1', cell_overwrite_ok=True) # 创建sheet
rowz=[u'产能报表']
row0 = [u'序号', u'公司', u'部门', u'换算后产能(双)', u'打卡折合后人数/月', u'申请工时/月', u'上班总工时/月', u'人均产能/天/人', u'电脑车比重', u'各厂总人均产能']
column0 = range(,sumnum+)
# msg0 =shuangchilist+shuanglianlist+shuangyuanlist+xiangchanglist
print(shuangyuanlist)
sheet1.col().width = 2222 # 单元格宽度
sheet1.col().width =
sheet1.col().width =
sheet1.col().width =
sheet1.col().width =
sheet1.col().width =
sheet1.col().width =
sheet1.col().width =
sheet1.col().width =
sheet1.col().width =
sheet1.col().width = sheet1.write_merge(,, , , rowz[], set_style('Times New Roman', , True)) for i in range(, len(row0)):
sheet1.write(, i, row0[i], set_style('Times New Roman', , True))
for j in range(len(shuangchilist)): for i in range(,len(shuangchilist[j])):
sheet1.write(j+, i+, shuangchilist[j][i], set_style('Times New Roman', , True)) #行,列,样式
for j in range(len(shuanglianlist)): for i in range(, len(shuanglianlist[j])):
sheet1.write(j + len(shuangchilist)+, i + , shuanglianlist[j][i], set_style('Times New Roman', , True))
for j in range(len(shuangyuanlist)): for i in range(, len(shuangyuanlist[j])):
sheet1.write(j + len(shuangchilist)+len(shuanglianlist)+, i + , shuangyuanlist[j][i], set_style('Times New Roman', , True))
for j in range(len(xiangchanglist)): for i in range(, len(xiangchanglist[j])):
sheet1.write(j + len(shuangchilist)+len(shuanglianlist)+len(shuangyuanlist)+, i + , xiangchanglist[j][i], set_style('Times New Roman', , True))
for i in range(, len(column0)):
sheet1.write(i + , , column0[i], set_style('Times New Roman', ))
f.save('demo1111111.xls') # 保存文件