W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
xlwt庫(kù)是一個(gè)python用于操作excel的第三方庫(kù)。它的主要功能是用來(lái)寫(xiě)入excel。通常會(huì)與xlrd 、 xlutils組合進(jìn)行使用。
注意!這里的操作excel,實(shí)際上與excel無(wú)關(guān),不需要下載excel,xlwt庫(kù)直接操作的是excel打開(kāi)的xls文件!注意!xlrd庫(kù)只能創(chuàng)建和修改excel,不能打開(kāi)excel!
可以使用pip進(jìn)行安裝,這是最簡(jiǎn)單也是最普遍的安裝方式!
在cmd中輸入?pip install xlrd?即可安裝xlrd庫(kù)。
安裝成功后可以使用?pip list?來(lái)檢查是否正確安裝以及查看當(dāng)前的xlrd版本。
注意:xlrd較高版本中不支持.xlsx文件,可以手動(dòng)將版本降至更低的版本或者將.xlsx文件改為.xls文件先卸載掉現(xiàn)有的 xlrd —> pip uninstall xlrd重新安裝指定版本的 xlrd —> pip install xlrd == 1.2.0接下來(lái)的文章由于此原因使用1.2.0版本的xlrd進(jìn)行介紹
import xlwt
# 創(chuàng)建一個(gè)workbook并設(shè)置編碼
workbook = xlwt.Workbook(encoding = 'utf-8')
# 添加sheet
worksheet = workbook.add_sheet('微課列表')
# 寫(xiě)入excel, 參數(shù)對(duì)應(yīng) 行, 列, 值
worksheet.write(1,0, label = 'MySQL零基礎(chǔ)入門(mén)課程')
# 保存
workbook.save('W3Cschool課程內(nèi)容.xls')
import xlwt
workbook = xlwt.Workbook(encoding = 'utf-8')
worksheet = workbook.add_sheet('微課列表')
# 初始化樣式
style = xlwt.XFStyle()
# 為樣式創(chuàng)建字體
font = xlwt.Font()
# 黑體
font.name = 'Times New Roman'
font.bold = True
# 下劃線
font.underline = True
# 斜體字
font.italic = True
# 設(shè)定樣式
style.font = font
# 帶樣式的寫(xiě)入
worksheet.write(0, 0, '帶樣式的單元格', style)
# 保存文件
workbook.save('W3Cschool課程內(nèi)容.xls')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('微課列表')
worksheet.write(1, 1,'MySQL')
# 設(shè)置單元格寬度
worksheet.col(0).width = 3000
workbook.save('W3Cschool課程內(nèi)容.xls')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('微課列表')
# 創(chuàng)建模式對(duì)象Create the Pattern
pattern = xlwt.Pattern()
# May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
#設(shè)置模式顏色 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
# 創(chuàng)建樣式對(duì)象Create the Pattern
style = xlwt.XFStyle()
# 將模式加入到樣式對(duì)象Add Pattern to Style
style.pattern = pattern
#向單元格寫(xiě)入內(nèi)容時(shí)使用樣式對(duì)象style
worksheet.write(0, 0, '單元格內(nèi)容', style)
workbook.save('W3Cschool課程內(nèi)容.xls')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('微課列表')
# 創(chuàng)建對(duì)其格式的對(duì)象 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
#我上下對(duì)齊 May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
alignment.vert = xlwt.Alignment.VERT_CENTER
#創(chuàng)建樣式對(duì)象 Create Style
style = xlwt.XFStyle()
# 將格式Alignment對(duì)象加入到樣式對(duì)象Add Alignment to Style
style.alignment = alignment
#寫(xiě)入的時(shí)候調(diào)用樣式style
worksheet.write(0, 0, '單元居中', style)
workbook.save('W3Cschool課程內(nèi)容.xls')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('')
worksheet.write(2, 3, xlwt.Formula('HYPERLINK("http://o2fo.com/minicourse/play/txymysql";"MySQL零基礎(chǔ)入門(mén)課程")'))
workbook.save('W3Cschool課程內(nèi)容.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]) 第二行第一列,輸出第一行數(shù)字乘積
worksheet.write(1, 0, xlwt.Formula('A1*B1'))
#(A1[5] + A2[2]) 第二行第二列,輸出第一行數(shù)字之和
worksheet.write(1, 1, xlwt.Formula('SUM(A1,B1)'))
workbook.save('W3Cschool課程內(nèi)容.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('W3Cschool課程內(nèi)容.xlsx')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('微課列表')
worksheet.write_merge(0, 1, 0, 3, '合并從第一行到第二行,第一列到第四列')
#創(chuàng)建字體示例
font = xlwt.Font()
#bold設(shè)置為黑體字
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('W3Cschool課程內(nèi)容.xls')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('微課列表')
# 創(chuàng)建邊框?qū)ο驝reate Borders
borders = xlwt.Borders()
#對(duì)邊框?qū)ο筮M(jìn)行操作,指定邊框上下左右的邊框類(lèi)型為虛線
# DASHED虛線
# NO_LINE沒(méi)有
# THIN實(shí)線
# 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 #創(chuàng)建樣式對(duì)象
style = xlwt.XFStyle()
# 將設(shè)置好的邊框?qū)ο骲orders 加到樣式對(duì)象style中。Add Borders to Style
style.borders = borders
#向單元格第一行第一列寫(xiě)入“單元格內(nèi)容”,并使用創(chuàng)建好的樣式對(duì)象style
worksheet.write(0, 0, '單元格內(nèi)容', style)
workbook.save('W3Cschool課程內(nèi)容.xls')
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: