新聞中心
為了表格的美觀和大方,我們需要改動列寬和行高。今天就為大家來的Python中xlwt的設(shè)置方法。

一:先創(chuàng)建一個excel
‘‘‘ Created on 2015-11-19 @author: Administrator ‘‘‘ import xlwt book = xlwt.Workbook(encoding=‘utf-8‘) sheet = book.add_sheet(‘sheet1‘)
二、設(shè)置列寬度
xlwt中列寬的值表示方法:默認(rèn)字體0的1/256為衡量單位。xlwt創(chuàng)建時使用的默認(rèn)寬度為2960,既11個字符0的寬度
所以我們在設(shè)置列寬時可以用如下方法:
width = 256 * 20 256為衡量單位,20表示20個字符寬度
那接下來完成我們的程序
#coding:utf-8 ‘‘‘ Created on 2015-11-19 @author: Administrator ‘‘‘ import xlwt book = xlwt.Workbook(encoding=‘utf-8‘) sheet = book.add_sheet(‘sheet1‘) first_col=sheet.col(0) #xlwt中是行和列都是從0開始計算的 sec_col=sheet.col(1) first_col.width=256*20 book.save(‘width.xls‘)
效果就如下:
三、行高
行寬是在單元格的樣式中設(shè)置的,你可以通過自動換行通過輸入文字的多少來確定行高
一般如下方法:
#coding:utf-8 ‘‘‘ Created on 2015-11-19 @author: Administrator ‘‘‘ import xlwt book = xlwt.Workbook(encoding=‘utf-8‘) sheet = book.add_sheet(‘sheet1‘) first_col=sheet.col(0) sec_col=sheet.col(1) first_col.width=256*20 tall_style = xlwt.easyxf(‘font:height 720;‘) # 36pt,類型小初的字號 first_row = sheet.row(0) first_row.set_style(tall_style) book.save(‘width.xls‘)
效果如下:
四、其它
在xlwt中沒有特定的函數(shù)來設(shè)置默認(rèn)的列寬及行高。
以上就是用xlwt設(shè)置表格列寬和行高的方法。更多Python學(xué)習(xí)推薦:PyThon學(xué)習(xí)網(wǎng)教學(xué)中心。
文章名稱:創(chuàng)新互聯(lián)Python教程:Python實用:用xlwt設(shè)置表格列寬和行高
網(wǎng)頁路徑:http://m.fisionsoft.com.cn/article/cdjosso.html


咨詢
建站咨詢
