1. 程式人生 > >Python中xlwt解析

Python中xlwt解析

works 指定 and 字體 distrib border eal orm puts

xlwt

1、導入模塊

import xlwt

2、構造excel表

workbook = xlwt.Workbook() #返回一個工作簿對象

3、構造sheet

workbook.add_sheet(‘info‘,cell_overwrite_ok=True) #添加name為info的sheet

4、構造sheet的格式

style=xlwt.XFStyle()

1) Formatting the Contents of a Cell

fnt = xlwt.Font()

fnt.name = u‘微軟雅黑‘

fnt.colour_index = 2

#0x01 # 字體顏色默認為黑色,此處設置字體顏色為白色, 顏色範圍為:0x00-0xff,也可

以純數字表示。

fnt.underline = True

fnt.italic = True

fnt.bold = True

fnt.height = 160 #font size:8pt

style.font = fnt

2) Setting the Background Color of a Cell

pattern = xlwt.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.pattern = pattern # Add Pattern to Style

3) Adding Borders to a Cell

border = xlwt.Borders()

border.left =xlwt.Borders.THIN

border.top =xlwt.Borders.THIN

border.right =xlwt.Borders.THIN

border.bottom =xlwt.Borders.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.

border.left_color = 0x40

border.top_color = 0x40

border.right_color = 0x40

border.bottom_color = 0x40

style.borders =border

4) Setting the Alignment for the Contents of a Cell

alignment = xlwt.Alignment()

style.alignment.horz = HORZ_CENTER #水平居中 值為2

style.alignment.vert = VERT_CENTER #垂直居中 值為1

style.alignment.wrap = 1 # 自動換行

style.alignment = alignment

#May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT,HORZ_FILLED,

HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED

sheet1.col(0).width = 256*20

#默認字體0的1/256為衡量單位。默認寬度為2960,既11個字符0的寬度。256為衡量單位,20表示20個字符寬度

5) Entering a Date into a Cell

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)

時間類型的轉換,把excel中時間轉成python 時間(兩種方式)

xlrd.xldate_as_tuple(table.cell(2,2).value, 0) #轉化為元組形式

xlrd.xldate.xldate_as_datetime(table.cell(2,2).value, 1) #直接轉化為datetime對象

xlrd.xldate_as_tuple(d,0)

#d 是從excel中讀取出來的浮點數

#第二個參數:有兩種取值,0是以1900-01-01為基準的日期,而1是1904-01-01為基準的日期該函數返回的是一個元組,他的值類似:(year, month, day, hour, minute, nearest_second)

6) Adding a Formula to a Cell

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

7) Merging Columns and Rows

worksheet.write_merge(0, 0, 0, 3, ‘First Merge‘)

# Merges row 0‘s columns 0 through 3.

worksheet.write_merge(1, 2, 0, 3, ‘Second Merge‘, style)

# Merges row 1 through 2‘s columns 0 through 3

8) Adding a Hyperlink to a Cell

worksheet.write(0,0,xlwt.Formula(‘HYPERLINK("http://www.google.com";"Google")‘))

# Outputs the text "Google" linking to http://www.google.com

5、寫入數據到sheet

sheet1.write(0,0,‘ok‘) #在指定行列寫入數據

6、保存excel

workbook.save(‘ok.xls‘) #對工作簿進行保存

Python中xlwt解析