python向xls寫入數(shù)據(jù)(包括合并,邊框,對齊,列寬)
1、常規(guī)寫入
# -*- encoding=utf-8 -*- import xlwt if __name__ == '__main__': head = ['姓名', '年齡', '出生年月'] data = [ ['蓋倫', '20', '2012-02-04'], ['趙信', '18', '2013-05-12'], ['女槍', '18', '2015-12-12'], ['劍圣', '20', '2012-11-14'], ] workbook = xlwt.Workbook() # 添加一個表, # cell_overwrite_ok=True表示覆蓋,如果下標(biāo)相同,則覆蓋,不寫,下標(biāo)相同,則拋出異常 sheet1 = workbook.add_sheet('Sheet1', cell_overwrite_ok=False) for index, info in enumerate(head): # 寫入表頭 sheet1.write(0, index, info) for index, row_data in enumerate(data): # 寫入數(shù)據(jù),注意拼接下標(biāo) for line, line_data in enumerate(row_data): sheet1.write(index + 1, line, line_data) sheet2 = workbook.add_sheet('Sheet2') # 添加一個表 for index, info in enumerate(head): # 寫入表頭 sheet2.write(0, index, info) for index, row_data in enumerate(data): # 寫入數(shù)據(jù),注意拼接下標(biāo) for line, line_data in enumerate(row_data): sheet2.write(index + 1, line, line_data) workbook.save('savexls.xls')
運(yùn)行后
2、合并單元格寫入
# -*- encoding=utf-8 -*- import xlwt if __name__ == '__main__': workbook = xlwt.Workbook() sheet1 = workbook.add_sheet('Sheet1') # 合并從0行到0行,從0列到1列 sheet1.write_merge(0, 0, 0, 1, '合并單元格') # 合并從2行到4行,從0列到3列 sheet1.write_merge(2, 4, 0, 3, '合并單元格') workbook.save('merge.xls')
運(yùn)行截圖
3、追加寫入
源xls文件
# -*- encoding=utf-8 -*- import xlrd from xlutils.copy import copy if __name__ == '__main__': pass filename = 'readxls.xls' f = xlrd.open_workbook(filename) # 打開Excel為xlrd對象 old_sheet = f.sheet_by_index(0) # 取到第一個舊表 old_sheet_rows = old_sheet.nrows # 第一個舊表的行數(shù),下面追加就得在這個后面寫入數(shù)據(jù) copy_read = copy(f) # 把xlrd對象轉(zhuǎn)為xlwt對象 new_sheet = copy_read.add_sheet('new_sheet') # 添加新表,表名不能重復(fù) head = ['name', 'age', 'birthday'] data = [[1, 2, 3], [4, '2019/02/01', 6], [7, 8, 9]] for index, info in enumerate(head): # 寫入表頭 new_sheet.write(0, index, info) for index, row_data in enumerate(data): # 寫入數(shù)據(jù),注意拼接下標(biāo) for line, line_data in enumerate(row_data): new_sheet.write(index + 1, line, line_data) exist_sheet = copy_read.get_sheet(0) # 取舊表 exist_sheet.write(old_sheet_rows, 0, '新數(shù)據(jù)1') exist_sheet.write(old_sheet_rows, 1, '新數(shù)據(jù)2') exist_sheet.write(old_sheet_rows, 2, '新數(shù)據(jù)3') copy_read.save('append.xlsx')
運(yùn)行截圖
4、設(shè)置對齊,邊框,列寬
# -*- encoding=utf-8 -*-import xlwtbook = xlwt.Workbook()sheet = book.add_sheet('sheet')sheet.write(6, 6, 'data')align = xlwt.Alignment()align.horz = xlwt.Alignment.HORZ_CENTER # 水平居中align.vert = xlwt.Alignment.VERT_CENTER # 垂直居中font = xlwt.Font() # 字體基本設(shè)置font.name = u'新宋體'font.colour_index = 32764 # 字體顏色font.height = 160 # 字體大小borders = xlwt.Borders()borders.left = xlwt.Borders.THIN # 添加邊框,細(xì)實(shí)線borders.right = xlwt.Borders.THINborders.top = xlwt.Borders.THINborders.bottom = xlwt.Borders.THINsheet.col(6).width = 12 * 256 # 設(shè)置列寬,一個中文等于兩個英文等于兩個字符,12為字符數(shù),256為衡量單位style = xlwt.XFStyle()style.font = fontstyle.alignment = alignstyle.borders = borderssheet.write(6, 8, 'data', style)book.save('style.xls')
以上就是python向xls寫入數(shù)據(jù)(包括合并,邊框,對齊,列寬)的詳細(xì)內(nèi)容,更多關(guān)于python向xls寫入數(shù)據(jù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決windows下Sublime Text 2 運(yùn)行 PyQt 不顯示的方法分享
問題描述:PyQt 環(huán)境正常,可以使用 Windows 的 虛擬 DOS 正常運(yùn)行,但在 Sublime Text 2 下使用 Ctrl + B 運(yùn)行后,界面不顯示,但查看任務(wù)管理器,有 python.exe 進(jìn)程。2014-06-06Python教程通過公共鍵對不同字典進(jìn)行排序示例詳解
本篇文章是Python教程基礎(chǔ)篇,通過一些示例為大家講解Python通過公共鍵對不同字典進(jìn)行排序的方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09Python實(shí)現(xiàn)暴力破解wifi密碼并打包成exe
python號稱是編程界的萬金油,那么是否可以做個讀取電腦網(wǎng)卡wifi并暴力破解的小腳本呢?在這個基礎(chǔ)上為了方便體驗(yàn)是不是可以將其打包成exe這樣方便執(zhí)行的小應(yīng)用呢?本文就來和大家一起聊聊2022-09-09python調(diào)用stitcher類自動實(shí)現(xiàn)多個圖像拼接融合功能
這篇文章主要介紹了python調(diào)用stitcher類自動實(shí)現(xiàn)多個圖像拼接融合功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04pytorch中的hook機(jī)制register_forward_hook
這篇文章主要介紹了pytorch中的hook機(jī)制register_forward_hook,手動在forward之前注冊hook,hook在forward執(zhí)行以后被自動執(zhí)行,下面詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下2022-03-03python web框架Flask實(shí)現(xiàn)圖形驗(yàn)證碼及驗(yàn)證碼的動態(tài)刷新實(shí)例
在本篇文章里小編給大家整理的是關(guān)于python web框架Flask實(shí)現(xiàn)圖形驗(yàn)證碼的相關(guān)知識點(diǎn),有需要的朋友們參考下。2019-10-10Python可視化Matplotlib散點(diǎn)圖scatter()用法詳解
這篇文章主要介紹了Python可視化中Matplotlib散點(diǎn)圖scatter()的用法詳解,文中附含詳細(xì)示例代碼,有需要得朋友可以借鑒參考下,希望能夠有所幫助2021-09-09Python數(shù)據(jù)結(jié)構(gòu)之列表與元組詳解
序列是Python中最基本的數(shù)據(jù)結(jié)構(gòu)。序列中的每個元素都分配一個數(shù)字 - 它的位置,或索引,第一個索引是0,第二個索引是1,依此類推,元組與列表類似,不同之處在于元組的元素不能修改。元組使用小括號,列表使用方括號2021-10-10詳解mac python+selenium+Chrome 簡單案例
這篇文章主要介紹了詳解mac python+selenium+Chrome 簡單案例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11