首页 > 编程语言 > 详细

python 读取Excel(二)之xlwt

时间:2017-06-09 23:12:21      阅读:422      评论:0      收藏:0      [点我收藏+]

     今天由于在接口测试报告中感觉自己写的接口测试报告特别low,Excel的连个颜色都不加,就想着怎么去想办法给整整,自己根据API一次次调试,感觉很慢,于是乎,百度,可惜没有找到,去官网,官网给的也特别的简单,

import xlwt
from datetime import datetime

style0 = xlwt.easyxf(font: name Times New Roman, color-index red, bold on,
    num_format_str=#,##0.00)
style1 = xlwt.easyxf(num_format_str=D-MMM-YY)

wb = xlwt.Workbook()
ws = wb.add_sheet(A Test Sheet)

ws.write(0, 0, 1234.56, style0)
ws.write(1, 0, datetime.now(), style1)
ws.write(2, 0, 1)
ws.write(2, 1, 1)
ws.write(2, 2, xlwt.Formula("A3+B3"))

wb.save(example.xls)

我们来看官网给的例子给我们展示什么样的效果,如图

技术分享

 

我们可以看到创建一个A Test Sheet的表。

首先我们先引入这个模块

import xlwt
接着呢,我们看到引入了
xlwt.easyxf,然后在style.py有这个函数,我们可以去看看这个函数,其实在这里给我们定义了很多的样式,我们只需要调用我们相拥的相应的函数传入相应的参数就可以,下面是我截取的一部分,
align: alignment, # synonym
    alignment: {
        dire: {
            general: 0,
            lr: 1,
            rl: 2,
            },
        direction: dire,
        horiz: horz,
        horizontal: horz,
        horz: {
            general: 0,
            left: 1,
            center: 2,
            centre: 2, # "align: horiz centre" means xf.alignment.horz is set to 2
            right: 3,
            filled: 4,
            justified: 5,
            center_across_selection: 6,
            centre_across_selection: 6,
            distributed: 7,
            },
        inde: IntULim(15), # restriction: 0 <= value <= 15
        indent: inde,
        rota: [{stacked: 255, none: 0, }, rotation_func],
        rotation: rota,
        shri: bool_map,
        shrink: shri,
        shrink_to_fit: shri,
        vert: {
            top: 0,
            center: 1,
            centre: 1,
            bottom: 2,
            justified: 3,
            distributed: 4,
            },
         vertical: vert,
         wrap: bool_map,
         },
    border: borders,
    borders: {
        left:     [border_line_map, IntULim(0x0d)],
        right:    [border_line_map, IntULim(0x0d)],
        top:      [border_line_map, IntULim(0x0d)],
        bottom:   [border_line_map, IntULim(0x0d)],
        diag:     [border_line_map, IntULim(0x0d)],
        top_colour:       [colour_map, colour_index_func_7],
        bottom_colour:    [colour_map, colour_index_func_7],
        left_colour:      [colour_map, colour_index_func_7],
        right_colour:     [colour_map, colour_index_func_7],
        diag_colour:      [colour_map, colour_index_func_7],
        top_color:        top_colour,
        bottom_color:     bottom_colour,
        left_color:       left_colour,
        right_color:      right_colour,
        diag_color:       diag_colour,
        need_diag1:  bool_map,
        need_diag2:  bool_map,
        },
    font: {
        bold: bool_map,
        charset: charset_map,
        color:  colour_index,
        color_index:  colour_index,
        colour:  colour_index,
        colour_index: [colour_map, colour_index_func_15],
        escapement: {none: 0, superscript: 1, subscript: 2},
        family: {none: 0, roman: 1, swiss: 2, modern: 3, script: 4, decorative: 5, },
        height: IntULim(0xFFFF), # practical limits are much narrower e.g. 160 to 1440 (8pt to 72pt)
        italic: bool_map,
        name: any_str_func,
        outline: bool_map,
        shadow: bool_map,
        struck_out: bool_map,
        underline: [bool_map, {none: 0, single: 1, single_acc: 0x21, double: 2, double_acc: 0x22, }],
        },
    pattern: {
        back_color:   pattern_back_colour,
        back_colour:  pattern_back_colour,
        fore_color:   pattern_fore_colour,
        fore_colour:  pattern_fore_colour,
        pattern: [pattern_map, IntULim(16)],
        pattern_back_color:   pattern_back_colour,
        pattern_back_colour:  [colour_map, colour_index_func_7],
        pattern_fore_color:   pattern_fore_colour,
        pattern_fore_colour:  [colour_map, colour_index_func_7],
        },
    protection: {
        cell_locked :   bool_map,
        formula_hidden: bool_map,
        },
    }

这里面给我们定义了很多样式,这些包括是很多文档都没有介绍的,API 文档,官网 github 也都没有给你做出来解释,在stackoverflow,上有了一些介绍,其实也是根据源码来介绍的,在学习python当中我们更多的是要学会去读源码,找到源码的实现逻辑,那么你学起来就是很简单的,下面我们来看看我修改后的代码,

import xlwt
from datetime import datetime
from xlwt import  *  #引入相应的库
style0 = xlwt.easyxf(‘font: name Times New Roman‘,
    num_format_str=‘#,##0.00‘,) #字体的颜色
styleOK = easyxf(‘pattern: fore_colour light_blue;‘
                          ‘font: colour green, bold True;‘)
pattern = xlwt.Pattern()#一个实例化的样式类
pattern.pattern = xlwt.Pattern.SOLID_PATTERN#固定的样式
pattern.pattern_fore_colour = xlwt.Style.colour_map[‘red‘]#背景颜色
styleOK.pattern = pattern
style1 = xlwt.easyxf(num_format_str=‘D-MMM-YY‘)#显示时间定义时间的样式
wb = xlwt.Workbook()
ws = wb.add_sheet(‘测试用‘)
ws.write(0, 0, 1234.56, style0)
ws.write(1, 0, datetime.now(), style1 )
ws.write(2, 0, 1,style=styleOK)
ws.write(2, 1, 1)
ws.write(2, 2, xlwt.Formula("A3+B3"))
wb.save(‘res.xls‘)

  

那么我们来看看生成的Excel表格

技术分享

感觉到可以规定的样式多了。是不是瞬间能够给你的Excel增加逼格,


import xlwt
from datetime import datetime
from xlwt import  *  #引入相应的库
style0 = xlwt.easyxf(font: name Times New Roman,
    num_format_str=#,##0.00,) #字体的颜色
styleOK = easyxf(pattern: fore_colour light_blue;
                          font: colour green, bold True;)
alignment = xlwt.Alignment()#设置居中
alignment.horz = xlwt.Alignment.HORZ_CENTER
alignment.vert = xlwt.Alignment.VERT_CENTER
style0.font.height = 280
style3 = XFStyle()
style3.alignment = alignment  # 给样式添加文字居中属性
style3.font.height = 330  # 设置字体大小
pattern = xlwt.Pattern()#一个实例化的样式类
pattern.pattern = xlwt.Pattern.SOLID_PATTERN#固定的样式
pattern.pattern_fore_colour = xlwt.Style.colour_map[red]#背景颜色
styleOK.pattern = pattern
style1 = xlwt.easyxf(num_format_str=DD-MM-YY)#显示时间定义时间的样式
wb = xlwt.Workbook()
ws = wb.add_sheet(测试用)
for i in  range(6):#定义列宽
    ws.col(i).width = 200*30
ws.write_merge(0, 0,0,5,测试报告, style3)
ws.write(1, 0,测试时间, style0 )
ws.write(1, 1, datetime.now(),style1)
ws.write(1, 2,测试人, style0 )
ws.write(1, 3,雷子, styleOK)
wb.save(res.xls)

技术分享

 

python 读取Excel(二)之xlwt

原文:http://www.cnblogs.com/leiziv5/p/6974078.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!