1、安装xlwt
pip install xlwt
2、写Excel必要的几步
import xlwt
book = xlwt.Workbook() #创建一个workbook,无编码设置编码
book = xlwt.Workbook(encoding = 'utf-8') #创建一个workbook,并设置编码 sheet = book.add_sheet('testsheet') # 创建一个sheet sheet.write(1,0, label='test',style) # 写入Excel操作,参数对应行、列、要写入的值,样式,样式可以不用带 book.save(self.file+"test.xls") # 保存Excel
运行后,会在file这个路径下生成一个“test.xls”的文件。
3、将时间写入到Excel单元格
import xlwt import datetime book = xlwt.Workbook() sheet = book.add_sheet('timetest') style = xlwt.XFStyle() style.num_format_str = 'YYYY-MM-DD hh:mm:ss' # 格式由自己选择,可自由指定 sheet.write(0, 3, datetime.datetime.now(), style) book.save('timetest.xls')
4、字体的操作
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
import xlwt book = xlwt.Workbook(encoding='utf-8') sheet = book.add_sheet('testsheet') # 设置字体 style = xlwt.XFStyle() font = xlwt.Font() font.bold = True # 字体加粗 font.name = 'Times New Roman' # 选择字体 font.underline = True # 字体下划线 font.italic = True # 斜体 font.height = 300 # 字体大小 font.colour_index = 3 # 字体颜色 style.font = font sheet.write(1,1,'This is a testcase') # 不带样式写入 sheet.write(2,1,'This is a testcase',style) # 带样式写入
book.save(file + '\test_excel.xls')
也可以换种方式:使用xlwt.easyxf
import xlwt book = xlwt.Workbook(encoding='utf-8') sheet = book.add_sheet('testsheet') style = xlwt.easyxf('font: colour_index red, bold on') # style = xlwt.easyxf('font: height 300') sheet.write(2,1,'This is a testcase',style) book.save(file + '\test_excel.xls')
5、设置单元格的宽、高
行宽设置:
xlwt中列宽的值表示方法:默认字体0的1/256为衡量单位。
xlwt创建时使用的默认宽度为2960,既11个字符0的宽度
我们在设置列宽时可以用此方法:width = 256 * 20 256为衡量单位,20表示20个字符宽度
import xlwt sheet = book.add_sheet('testsheet') sheet.write(1,1,'This is a testcase') sheet.col(0).width = 5120 # 设置单元格宽度 book.save(file + '\test_excel.xls')
行高设置:
行高可以不设定,会根据自己设置字体的大小自动适应行高
6、为单元格设置背景色
may be :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 or
import xlwt book = xlwt.Workbook(encoding='utf-8') sheet = book.add_sheet('testsheet') style = xlwt.XFStyle() sheet.col(1).width = 5120 bg = xlwt.Pattern() bg.pattern = bg.SOLID_PATTERN # NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12 bg.pattern_fore_colour = 3 style.pattern = bg sheet.write(2,1,'This is a testcase',style) book.save(file + '\test_excel.xls')
设置背景色也可以换一种方式:使用xlwt.easyxf
import xlwt book = xlwt.Workbook(encoding='utf-8') sheet = book.add_sheet('testsheet') style = xlwt.easyxf('pattern: pattern solid, fore_colour red') sheet.write(2,1,'This is a testcase',style) book.save(file + '\test_excel.xls')
7、为单元格添加边框
borders的分类大致分为:NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR, MEDIUM_DASHED, THIN_DASH_DOTTED, MEDIUM_DASH_DOTTED, THIN_DASH_DOT_DOTTED, MEDIUM_DASH_DOT_DOTTED, SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D.
import xlwt book = xlwt.Workbook(encoding='utf-8') sheet = book.add_sheet('testsheet') style = xlwt.XFStyle() sheet.col(1).width = 5120 borders = xlwt.Borders() borders.left = borders.DASHED # DASHED虚线 # NO_LINE没有 # THIN实线 borders.right = borders.THIN borders.top = borders.NO_LINE borders.bottom = borders.DOUBLE borders.left_colour = 6 borders.right_colour = 4 borders.top_colour = 4 borders.bottom_colour = 3 style.borders = borders sheet.write(2,1,'This is a testcase',style) book.save(file + '\test_excel.xls')
8、设置单元格里面内容的对其方式
单元格对其方式有两种:水平对齐(hoze)和垂直对齐(vert)
水平对齐包含: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
垂直对齐包含:VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
import xlwt book = xlwt.Workbook(encoding='utf-8') sheet = book.add_sheet('testsheet') aligment = xlwt.Alignment() aligment.horz = aligment.HORZ_CENTER # 水平对齐方式 aligment.vert = aligment.VERT_BOTTOM # 垂直对其方式 style.alignment = aligment sheet.write(2,1,'This is a testcase',style) book.save(file + '\test_excel.xls')
9、合并单元格行、列
合并单元格是在写入的时候合并,使用write_merge,相对于write来说里面增加了两个参数,即合并的行和列。write_merge参数的前两个代表要合并的行,三到四位参数代表的是要合并的列。
import xlwt book = xlwt.Workbook(encoding='utf-8') sheet = book.add_sheet('testsheet') sheet.write_merge(1,3,1,1,'This is a testcase') # 合并1-3行的1-1列,前两个参数代表行,后两个参数代表列 book.save(file + '\test_excel.xls')
10、向单元格添加公式
向单元格中写公式时,通过xlwt.Formula添加公式
import xlwt book = xlwt.Workbook(encoding='utf-8') sheet = book.add_sheet('testsheet')
sheet.write(0,0,5) sheet.write(0,1,6) sheet.write(1,0,xlwt.Formula('A1*B1')) # A1[5]*B1[6]=30 sheet.write(2,0,xlwt.Formula('SUM(A1:A2)')) # A1[5]+A2[30]=35 book.save(file + '\test_excel.xls'
11、向单元格添加超链接
import xlwt book = xlwt.Workbook(encoding='utf-8') sheet = book.add_sheet('testsheet') sheet.write(0,0,xlwt.Formula('HYPERLINK("https://www.baidu.com";"百度")')) # 向单元格写入文字:百度,并加上链接:https://www.baidu.com