1. 程式人生 > >[Python]python之xlwt模組列寬width、行高Heights詳解

[Python]python之xlwt模組列寬width、行高Heights詳解

轉自:http://www.3fwork.com/b204/001224MYM011551/

python之xlwt模組列寬width、行高Heights詳解 今天用python操作excel時,發現xlwt的API中沒有對width、height有更多介紹,且使用時也不知道width取多少合適。現在這做個詳細介紹
使用版本:
python:2.7.5
xlwt:1.0.0
一:先建立一個excel

'''
Created on 2015-11-19
@author: Administrator
'''
import xlwt
book = xlwt.Workbook(encoding='utf-8')
sheet = book.add_sheet('
sheet1')

二、設定列寬度
xlwt中列寬的值表示方法:預設字型0的1/256為衡量單位。
xlwt建立時使用的預設寬度為2960,既11個字元0的寬度
所以我們在設定列寬時可以用如下方法:
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')

效果就如下:


三、行高
行寬是在單元格的樣式中設定的,你可以通過自動換行通過輸入文字的多少來確定行高
一般如下方法:

#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中沒有特定的函式來設定預設的列寬及行高
參考文件:
http://reliablybroken.com/b/2011/10/widths-heights-with-xlwt-python/