1. 程式人生 > >python 讀取Excel(二)之xlwt

python 讀取Excel(二)之xlwt

ocs xlwt direct shrink 增加 height func int 代碼

  今天由於在接口測試報告中感覺自己寫的接口測試報告特別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