利用Python中xlwt模塊操作excel的示例詳解

一、安裝

pip install xlwt

二、創建表格並寫入

import xlwt
 
# 創建一個workbook並設置編碼
workbook = xlwt.Workbook(encoding = 'utf-8')
 
# 添加sheet
worksheet = workbook.add_sheet('飛兔小哥')
 
# 寫入excel, 參數對應 行, 列, 值
worksheet.write(1,0, label = '我愛autofelix')
 
# 保存
workbook.save('xlwt教學.xls')

三、設置單元格樣式

import xlwt
 
workbook = xlwt.Workbook(encoding = 'ascii')
worksheet = workbook.add_sheet('飛兔小哥')
 
 # 初始化樣式
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, '帶樣式的單元格', style)
 
# 保存文件
workbook.save('xwlt單元格樣式教學.xls')

四、設置單元格寬度

import xlwt
 
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('飛兔小哥')
 
worksheet.write(0, 0,'我愛autofelix')
 
# 設置單元格寬度
worksheet.col(0).width = 3000
 
workbook.save('xwlt單元格寬度教學.xls')

五、設置單元格背景色

import xlwt
 
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('飛兔小哥')
 
# 創建模式對象Create the Pattern
pattern = xlwt.Pattern()
 
# May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
 
#設置模式顏色 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...
pattern.pattern_fore_colour = 5
 
# 創建樣式對象Create the Pattern
style = xlwt.XFStyle()
 
# 將模式加入到樣式對象Add Pattern to Style
style.pattern = pattern
 
#向單元格寫入內容時使用樣式對象style
worksheet.write(0, 0, '單元格內容', style)
 
workbook.save('xwlt單元格背景色教學.xls')

六、設置單元格內容對齊方式

import xlwt
 
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('飛兔小哥')
 
# 創建對其格式的對象 Create Alignment
alignment = xlwt.Alignment()
 
#水平居中 May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
alignment.horz = xlwt.Alignment.HORZ_CENTER
 
#我上下對齊 May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
alignment.vert = xlwt.Alignment.VERT_CENTER 
 
#創建樣式對象 Create Style
style = xlwt.XFStyle()
 
# 將格式Alignment對象加入到樣式對象Add Alignment to Style
style.alignment = alignment
 
#寫入的時候調用樣式style
worksheet.write(0, 0, '單元居中', style)
 
workbook.save('xlwt單元格對齊教學.xls')

七、單元格添加超鏈接

import xlwt
 
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('飛兔小哥')
 
worksheet.write(0, 0, xlwt.Formula('HYPERLINK("https://www.baidu.com";"百度")'))
 
workbook.save('xwlt單元格超鏈接教學.xls')

八、單元格添加公式

import xlwt
 
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('飛兔小哥')
 
worksheet.write(0, 0, 5) # Outputs 5  #第一行第一列5
worksheet.write(0, 1, 2) # Outputs 2  #第一行第二列2
 
#(A1[5] * A2[2])  第二行第一列,輸出第一行數字乘積
worksheet.write(1, 0, xlwt.Formula('A1*B1'))
 
#(A1[5] + A2[2])  第二行第二列,輸出第一行數字之和
worksheet.write(1, 1, xlwt.Formula('SUM(A1,B1)'))
 
workbook.save('xwlt添加公式教學.xls')

九、單元格中輸入日期

import xlwt
import datetime
 
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('飛兔小哥')
 
style = xlwt.XFStyle()
 
#其他格式包括: D-MMM-YY, D-MMM, MMM-YY, h:mm, h:mm:ss, h:mm, h:mm:ss, M/D/YY h:mm, mm:ss, [h]:mm:ss, mm:ss.0
style.num_format_str = 'M/D/YY'
 
worksheet.write(0, 0, datetime.datetime.now(), style)
 
workbook.save('xwlt日期格式教學.xlsx')

十、合並行和列

import xlwt
 
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('飛兔小哥')
 
worksheet.write_merge(0, 1, 0, 3, '合並從第一行到第二行,第一列到第四列')
 
#創建字體示例
font = xlwt.Font()
 
#bold設置為黑體字
font.bold = True
 
# Create Style
style = xlwt.XFStyle()
 
# Add Bold Font to Style
style.font = font
worksheet.write_merge(2,3, 0, 3, '合並從第三行到第四行,第一列到第四列', style)
 
workbook.save('xwlt合並單元格教學.xls')

十一、單元格添加邊框

import xlwt
 
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('飛兔小哥')
 
# 創建邊框對象Create Borders
borders = xlwt.Borders()
 
#對邊框對象進行操作,指定邊框上下左右的邊框類型為虛線
# 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.left = xlwt.Borders.DASHED
borders.right = xlwt.Borders.DASHED
borders.top = xlwt.Borders.DASHED
borders.bottom = xlwt.Borders.DASHED
 
#指定上下左右的邊框顏色為0x40
borders.left_colour = 0x40
borders.right_colour = 0x40
borders.top_colour = 0x40
borders.bottom_colour = 0x40
 
# Create Style   #創建樣式對象
style = xlwt.XFStyle()
 
# 將設置好的邊框對象borders 加到樣式對象style中。Add Borders to Style
style.borders = borders
 
#向單元格第一行第一列寫入“單元格內容”,並使用創建好的樣式對象style
worksheet.write(0, 0, '單元格內容', style)
workbook.save('xwlt添加邊框教學.xls')

以上就是利用Python中xlwt模塊操作excel的示例詳解的詳細內容,更多關於Python xlwt模塊操作excel的資料請關註WalkonNet其它相關文章!

推薦閱讀: