安装:pip install xlwt
写入支持所有表格相关设置,用于新建表格
基本设置:
import xlwt import xlrd import datetime #设置表格编码格式 book = xlwt.Workbook(encoding='utf-8') #设置新增sheet页名称 test1 = book.add_sheet(u'test') test2 = book.add_sheet(u'test2') #初始化样式 style = xlwt.XFStyle() #创建字体,name字体名 bold是否黑体 underline 下划线 italic斜体 font = xlwt.Font() font.name = 'Time New Roman' font.bold = True font.underline = True font.italic = True style.font = font #设置以上格式 style.num_format_str = 'YY/M/D' #设置日期格式 #输入某个位置参数值 test1.write(0,0,'时间',style) #带设定格式的输入 test2.write(0,0,'aaa') #不带设定格式的输入 #设置单个列宽和行高 test1.col(0).width = 3000 test1.row(0).height = 200 #输入时间 test1.write(1,0,datetime.datetime.now(),style) #保存到指定目录 book.save("C:/Users/Lenovo/Desktop/test.xls")
输出:
输入公式:
输出结果:
插入超链接