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/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM