您好,登錄后才能下訂單哦!
這篇文章主要介紹了Python中xlwt設置單元格格式的方法,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
很多人對編程的印象都是比較枯燥無味,但是其中好玩的也很多呀。今天我們就來通過改變單元格式,讓表格變得賞心悅目。
使用xlwt中的Alignment來設置單元格的對齊方式,其中horz代表水平對齊方式,vert代表垂直對齊方式。
VERT_TOP = 0x00 上端對齊
VERT_CENTER = 0x01 居中對齊(垂直方向上)
VERT_BOTTOM = 0x02 低端對齊
HORZ_LEFT = 0x01 左端對齊
HORZ_CENTER = 0x02 居中對齊(水平方向上)
HORZ_RIGHT = 0x03 右端對齊
import xlwt workbook = xlwt.Workbook(encoding = ‘ascii‘) worksheet = workbook.add_sheet(‘My Worksheet‘) style = xlwt.XFStyle() # 初始化樣式 font = xlwt.Font() # 為樣式創建字體 font.name = ‘Times New Roman‘ font.bold = True # 黑體 font.underline = True # 下劃線 font.italic = True # 斜體字 style.font = font # 設定樣式 worksheet.write(0, 0, ‘Unformatted value‘) # 不帶樣式的寫入 worksheet.write(1, 0, ‘Formatted value‘, style) # 帶樣式的寫入 workbook.save(‘formatting.xls‘) # 保存文件
設置寬度
import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet(‘My Sheet‘) worksheet.write(0, 0,‘My Cell Contents‘) # 設置單元格寬度 worksheet.col(0).width = 3333 workbook.save(‘cell_width.xls‘)
添加超鏈接
import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet(‘My Sheet‘) worksheet.write(0, 0, xlwt.Formula(‘HYPERLINK("http://www.google.com";"Google")‘)) # Outputs the text "Google" linking to http://www.google.com workbook.save(‘Excel_Workbook.xls‘)
對其方式
import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet(‘My Sheet‘) alignment = xlwt.Alignment() # Create Alignment alignment.horz = xlwt.Alignment.HORZ_CENTER # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED style = xlwt.XFStyle() # Create Style style.alignment = alignment # Add Alignment to Style worksheet.write(0, 0, ‘Cell Contents‘, style) workbook.save(‘Excel_Workbook.xls‘)
添加邊框
import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet(‘My Sheet‘) borders = xlwt.Borders() # Create Borders borders.left = xlwt.Borders.DASHED DASHED虛線 NO_LINE沒有 THIN實線 # May be: NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR, MEDIUM_DASHED, THIN_DASH_DOTTED, MEDIUM_DASH_DOTTED, THIN_DASH_DOT_DOTTED, MEDIUM_DASH_DOT_DOTTED, SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D. borders.right = xlwt.Borders.DASHED borders.top = xlwt.Borders.DASHED borders.bottom = xlwt.Borders.DASHED borders.left_colour = 0x40 borders.right_colour = 0x40 borders.top_colour = 0x40 borders.bottom_colour = 0x40 style = xlwt.XFStyle() # Create Style style.borders = borders # Add Borders to Style worksheet.write(0, 0, ‘Cell Contents‘, style) workbook.save(‘Excel_Workbook.xls‘)
設置背景色顏色
import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet(‘My Sheet‘) pattern = xlwt.Pattern() # Create the Pattern pattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12 pattern.pattern_fore_colour = 5 # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on... style = xlwt.XFStyle() # Create the Pattern style.pattern = pattern # Add Pattern to Style worksheet.write(0, 0, ‘Cell Contents‘, style) workbook.save(‘Excel_Workbook.xls‘)
感謝你能夠認真閱讀完這篇文章,希望小編分享Python中xlwt設置單元格格式的方法內容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。