python xlwt模塊使用

安裝html

$ pip install xlwt
python

例子:ide

import xlwt
字體

# 建立一個workbook 設置編碼this

workbook = xlwt.Workbook(encoding = 'utf-8')google

# 建立一個worksheet編碼

worksheet = workbook.add_sheet('My Worksheet')spa

# 寫入excel
excel

# 參數對應 行, 列, 值code

worksheet.write(1,0, label = 'this is test')

# 保存

workbook.save('Excel_test.xls')

運行後 會在當前目錄生成一個Excel_test.xls


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') # 保存文件

效果:

5796306-b8f7b88c519259d4.jpg

設置單元格寬度:

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

import datetime

workbook = xlwt.Workbook()

worksheet = workbook.add_sheet('My Sheet')

style = xlwt.XFStyle()

style.num_format_str = 'M/D/YY' # Other options: 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

worksheet.write(0, 0, datetime.datetime.now(), style)

workbook.save('Excel_Workbook.xls')

向單元格添加一個公式:


import xlwt

workbook = xlwt.Workbook()

worksheet = workbook.add_sheet('My Sheet')

worksheet.write(0, 0, 5) # Outputs 5

worksheet.write(0, 1, 2) # Outputs 2

worksheet.write(1, 0, xlwt.Formula('A1*B1')) # Should output "10" (A1[5] * A2[2])

worksheet.write(1, 1, xlwt.Formula('SUM(A1,B1)')) # Should output "7" (A1[5] + A2[2])

workbook.save('Excel_Workbook.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')

worksheet.write_merge(0, 0, 0, 3, 'First Merge') # Merges row 0's columns 0 through 3.

font = xlwt.Font() # Create Font

font.bold = True # Set font to Bold

style = xlwt.XFStyle() # Create Style

style.font = font # Add Bold Font to Style

worksheet.write_merge(1, 2, 0, 3, 'Second Merge', style) # Merges row 1 through 2's columns 0 through 3.

workbook.save('Excel_Workbook.xls')


'''
須要稍做解釋的就是write_merge方法:

write_merge(x, x + m, y, w + n, string, sytle)
x表示行,y表示列,m表示跨行個數,n表示跨列個數,string表示要寫入的單元格內容,style表示單元格樣式。其中,xywh,都是以0開始計算

的。

這個和xlrd中的讀合併單元格的不太同樣。

如上述:sheet1.write_merge(21,21,0,1,u'合計',set_style('Times New Roman',220,True))

即在22行合併第1,2列,合併後的單元格內容爲"合計",並設置了style
'''


設置單元格內容的對齊方式:

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')


爲單元格議添加邊框:

# Please note: While I was able to find these constants within the source code, on my system (using LibreOffice,) I was only presented with a solid line, varying from thin to thick; no dotted or dashed lines.

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')

轉載:https://www.cnblogs.com/python-robot/p/9958352.html

還有參考:https://www.cnblogs.com/linyfeng/p/7123423.html

相關文章
相關標籤/搜索