Python xlrd/xlwt 創(chuàng)建excel文件及常用操作
一、創(chuàng)建excel代碼
備注:封裝好了(可直接調(diào)用)
'''-*- coding:utf-8 -*-@Time :2020/8/20 21:02@Author :Jarvis@File :jar_excel_util.py@Version:1.0'''from typing import Listimport xlwtclass JarExcelUtil: def __init__(self, header_list: List[list]): ''' :param header_list: 如下格式 例1:默認列寬 header_list = [ [’序號’], # 表格第0列[此列表頭名稱] [’姓名’], [’性別’], [’愛好’], [’生日’] ] 例2:自定義列寬(列寬值為int類型 英文字符長度 如:10 表示列寬為10個英文字符長度) header = [ [’序號’, 5], # 表格第0列[此列表頭名稱,列寬] [’姓名’, 10], # 表格第1列[此列表頭名稱,列寬] [’性別’, 10], [’愛好’, 10], [’生日’, 20] ] ''' self.data = header_list self.__color_str = ’aqua 0x31rnblack 0x08rnblue 0x0Crnblue_gray 0x36rnbright_green 0x0Brnbrown 0x3Crncoral 0x1Drncyan_ega 0x0Frndark_blue 0x12rndark_blue_ega 0x12rndark_green 0x3Arndark_green_ega 0x11rndark_purple 0x1Crndark_red 0x10rndark_red_ega 0x10rndark_teal 0x38rndark_yellow 0x13rngold 0x33rngray_ega 0x17rngray25 0x16rngray40 0x37rngray50 0x17rngray80 0x3Frngreen 0x11rnice_blue 0x1Frnindigo 0x3Ernivory 0x1Arnlavender 0x2Ernlight_blue 0x30rnlight_green 0x2Arnlight_orange 0x34rnlight_turquoise 0x29rnlight_yellow 0x2Brnlime 0x32rnmagenta_ega 0x0Ernocean_blue 0x1Ernolive_ega 0x13rnolive_green 0x3Brnorange 0x35rnpale_blue 0x2Crnperiwinkle 0x18rnpink 0x0Ernplum 0x3Drnpurple_ega 0x14rnred 0x0Arnrose 0x2Drnsea_green 0x39rnsilver_ega 0x16rnsky_blue 0x28rntan 0x2Frnteal 0x15rnteal_ega 0x15rnturquoise 0x0Frnviolet 0x14rnwhite 0x09rnyellow 0x0D’ self.color_list = [] # [[]] [[’aqua’, ’0x31’], [’black’, ’0x08’], ...] for color in self.__color_str.split(’rn’): color = color.split(’ ’) self.color_list.append(color) def write(self, out_file, data_body: List[list], sheet_name=’sheet’, frozen_row: int = 1, frozen_col: int = 0): ''' 寫入數(shù)據(jù) :param out_file: 保存文件(如:test.xlsx) :param data_body: data_body[0]為表格第0行數(shù)據(jù) data_body[0][0]為表格第0行第0列單元格值 :param sheet_name: :param frozen_row: 凍結(jié)行(默認首行) :param frozen_col: 凍結(jié)列(默認不凍結(jié)) ''' # step1 判斷數(shù)據(jù)正確性(每行列數(shù)是否與表頭相同) count = 0 for pro in data_body: if len(pro) != len(self.data): raise Exception( ’data_body數(shù)據(jù)錯誤 第{}行(從0開始) 需為{}個元素 當前行{}個元素:{}’.format(count, len(self.data), len(pro), str(pro))) count += 1 # step2 寫入數(shù)據(jù) wd = xlwt.Workbook() sheet = wd.add_sheet(sheet_name) ali_horiz = ’align: horiz center’ # 水平居中 ali_vert = ’align: vert center’ # 垂直居中 fore_colour = ’pattern: pattern solid,fore_colour pale_blue’ # 設(shè)置單元格背景色為pale_blue色 # 表頭格式(垂直+水平居中、表頭背景色) style_header = xlwt.easyxf(’{};{};{}’.format(fore_colour, ali_horiz, ali_vert)) # 表體格式(垂直居中) style_body = xlwt.easyxf(’{}’.format(ali_vert)) # 表頭 for col in self.data: # 默認列寬 if len(col) == 1: sheet.write(0, self.data.index(col), str(col[0]), style_header) # 自定義列寬 if len(col) == 2: sheet.write(0, self.data.index(col), str(col[0]), style_header) # 設(shè)置列寬 sheet.col(self.data.index(col)).width = 256 * col[1] # 256為基數(shù) * n個英文字符 # 行高(第0行) sheet.row(0).height_mismatch = True sheet.row(0).height = 20 * 20 # 20為基數(shù) * 20榜 # 表體 index = 1 for pro in data_body: sheet.row(index).height_mismatch = True sheet.row(index).height = 20 * 20 # 20為基數(shù) * 20榜 for d in self.data: value = pro[self.data.index(d)] # 若值類型是int、float 直接寫入 反之 轉(zhuǎn)成字符串寫入 if type(value) == int or type(value) == float: sheet.write(index, self.data.index(d), value, style_body) else: sheet.write(index, self.data.index(d), str(value), style_body) index += 1 # 凍結(jié)(列與行) sheet.set_panes_frozen(’1’) sheet.set_horz_split_pos(frozen_row) # 凍結(jié)前n行 sheet.set_vert_split_pos(frozen_col) # 凍結(jié)前n列 wd.save(out_file) def color_test(self): ''' 測試顏色 ''' body_t = [] for color in self.color_list: print(color) body_t.append(color) wd = xlwt.Workbook() sheet = wd.add_sheet(’sheet’) index = 0 for b in body_t: ali = ’align: horiz center;align: vert center’ # 垂直居中 水平居中 fore_colour = ’pattern: pattern solid,fore_colour {}’.format( self.color_list[index][0]) # 設(shè)置單元格背景色為pale_blue色 style_header = xlwt.easyxf( ’{};{}’.format(fore_colour, ali)) sheet.write(index, 0, str(b), style_header) sheet.col(0).width = 256 * 150 # 256為基數(shù) * n個英文字符 index += 1 wd.save(’顏色測試.xlsx’)# 測試顏色# if __name__ == ’__main__’:# header_t = [# [’顏色’]# ]# JarExcelUtil(header_t).color_test()if __name__ == ’__main__’: header = [ [’序號’, 5], [’姓名’, 10], [’性別’, 10], [’愛好’, 10], [’生日’, 20] ] # header = [ # [’序號’], # [’姓名’], # [’性別’], # [’愛好’], # [’生日’] # ] body = [ [1, ’張三’, ’男’, ’籃球’, ’1994-07-23’], [2, ’李四’, ’女’, ’足球’, ’1994-04-03’], [3, ’王五’, ’男’, ’兵乓球’, ’1994-09-13’] ] JarExcelUtil(header_list=header).write(out_file=’測試.xlsx’, data_body=body)
二、效果
生成的Excel
三、常用操作
3.1、設(shè)置行高
# 行高(第0行)sheet.row(0).height_mismatch = Truesheet.row(0).height = 20 * 20 # 20為基數(shù) * 20榜
3.2、設(shè)置列寬
# 列寬(第0列)sheet.col(0).width = 256 * 30 # 256為基數(shù) * 30個英文字符(約)
3.3、凍結(jié)(列與行)
# 凍結(jié)(列與行)sheet.set_panes_frozen(’1’)sheet.set_horz_split_pos(2) # 凍結(jié)前2行sheet.set_vert_split_pos(3) # 凍結(jié)前3列 # 凍結(jié)首行sheet.set_panes_frozen(’1’)sheet.set_horz_split_pos(1) # 凍結(jié)前1行(即首行)
3.4、設(shè)置單元格對齊方式
# 方式1style_1 = xlwt.XFStyle()al_1 = xlwt.Alignment()al_1.horz = xlwt.Alignment.HORZ_CENTER # 水平居中al_1.vert = xlwt.Alignment.VERT_CENTER # 垂直居中style_1.alignment = al_1sheet.write(0, 0, ’第0行第0列單元格值’, style_1) # 方式2(推薦)ali_horiz = ’align: horiz center’ # 水平居中ali_vert = ’align: vert center’ # 垂直居中style_2 = xlwt.easyxf(’{};{}’.format(ali_horiz, ali_vert))sheet.write(0, 0, ’第0行第0列單元格值’, style_2)
3.5、設(shè)置單元格背景色
# 設(shè)置單元格背景色fore_colour = ’pattern: pattern solid,fore_colour pale_blue’ # 設(shè)置單元格背景色為pale_blue色 (具體顏色值 參考上面代碼JarExcelUtil類中的color_test方法的運行結(jié)果)style = xlwt.easyxf(’{}’.format(fore_colour))sheet.write(0, 0, ’第0行第0列單元格值’, style)
以上就是Python xlrd/xlwt 創(chuàng)建excel文件及常用操作的詳細內(nèi)容,更多關(guān)于python 操作excel的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. jsp實現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫的方法2. ASP實現(xiàn)加法驗證碼3. asp知識整理筆記4(問答模式)4. IntelliJ IDEA 統(tǒng)一設(shè)置編碼為utf-8編碼的實現(xiàn)5. 解決ajax的delete、put方法接收不到參數(shù)的問題方法6. IntelliJ IDEA 2020最新激活碼(親測有效,可激活至 2089 年)7. jsp EL表達式詳解8. 詳解idea中web.xml默認版本問題解決9. idea開啟代碼提示功能的方法步驟10. java 優(yōu)雅關(guān)閉線程池的方案
