python xlwt 設置 格式

轉載自:
關于寫excel的格式控制纪隙,比如顏色等等

[
復制代碼

](javascript:void(0); "復制代碼")

<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 import xlwt 2 from datetime import datetime 3
4 font0 = xlwt.Font() 5 font0.name = 'Times New Roman'
6 font0.colour_index = 2
7 font0.bold = True 8
9 style0 = xlwt.XFStyle() 10 style0.font = font0 11
12 style1 = xlwt.XFStyle() 13 style1.num_format_str = 'D-MMM-YY'
14
15 wb = xlwt.Workbook() 16 ws = wb.add_sheet('A Test Sheet') 17
18 ws.write(0, 0, 'Test', style0) 19 ws.write(1, 0, datetime.now(), style1) 20 ws.write(2, 0, 1) 21 ws.write(2, 1, 1) 22 ws.write(2, 2, xlwt.Formula("A3+B3")) 23
24 wb.save('example.xls')</pre>

[
復制代碼

](javascript:void(0); "復制代碼")

Examples Generating Excel Documents Using Python’s xlwt

Here are some simple examples using Python’s xlwt library to dynamically generate Excel documents.

Please note a useful alternative may be ezodf, which allows you to generate ODS (Open Document Spreadsheet) files for LibreOffice / OpenOffice. You can check them out at:http://packages.python.org/ezodf/index.html

The Simplest Example
import xlwt workbook = xlwt.Workbook(encoding = 'ascii') worksheet = workbook.add_sheet('My Worksheet') worksheet.write(0, 0, label = 'Row 0, Column 0 Value') workbook.save('Excel_Workbook.xls')

Formatting the Contents of a Cell
import xlwt workbook = xlwt.Workbook(encoding = 'ascii') worksheet = workbook.add_sheet('My Worksheet') font = xlwt.Font() # Create the Font font.name = 'Times New Roman' font.bold = True font.underline = True font.italic = True style = xlwt.XFStyle() # Create the Style style.font = font # Apply the Font to the Style worksheet.write(0, 0, label = 'Unformatted value') worksheet.write(1, 0, label = 'Formatted value', style) # Apply the Style to the Cell workbook.save('Excel_Workbook.xls')

Attributes of the Font Object
font.bold = True # May be: True, False font.italic = True # May be: True, False font.struck_out = True # May be: True, False font.underline = xlwt.Font.UNDERLINE_SINGLE # May be: UNDERLINE_NONE, UNDERLINE_SINGLE, UNDERLINE_SINGLE_ACC, UNDERLINE_DOUBLE, UNDERLINE_DOUBLE_ACC font.escapement = xlwt.Font.ESCAPEMENT_SUPERSCRIPT # May be: ESCAPEMENT_NONE, ESCAPEMENT_SUPERSCRIPT, ESCAPEMENT_SUBSCRIPT font.family = xlwt.Font.FAMILY_ROMAN # May be: FAMILY_NONE, FAMILY_ROMAN, FAMILY_SWISS, FAMILY_MODERN, FAMILY_SCRIPT, FAMILY_DECORATIVE font.charset = xlwt.Font.CHARSET_ANSI_LATIN # May be: CHARSET_ANSI_LATIN, CHARSET_SYS_DEFAULT, CHARSET_SYMBOL, CHARSET_APPLE_ROMAN, CHARSET_ANSI_JAP_SHIFT_JIS, CHARSET_ANSI_KOR_HANGUL, CHARSET_ANSI_KOR_JOHAB, CHARSET_ANSI_CHINESE_GBK, CHARSET_ANSI_CHINESE_BIG5, CHARSET_ANSI_GREEK, CHARSET_ANSI_TURKISH, CHARSET_ANSI_VIETNAMESE, CHARSET_ANSI_HEBREW, CHARSET_ANSI_ARABIC, CHARSET_ANSI_BALTIC, CHARSET_ANSI_CYRILLIC, CHARSET_ANSI_THAI, CHARSET_ANSI_LATIN_II, CHARSET_OEM_LATIN_I font.colour_index = ? font.get_biff_record = ? font.height = 0x00C8 # C8 in Hex (in decimal) = 10 points in height. font.name = ? font.outline = ? font.shadow = ?

Setting the Width of a Cell
import xltw workbook = xlwt.Workbook() worksheet = workbook.add_sheet('My Sheet') worksheet.write(0, 0, 'My Cell Contents') worksheet.col(0).width = 3333 # 3333 = 1" (one inch). workbook.save('Excel_Workbook.xls')

Entering a Date into a Cell
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')

Adding a Formula to a Cell
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')

Adding a Hyperlink to a Cell
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')

Merging Columns and Rows
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')

Setting the Alignment for the Contents of a Cell
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')

Adding Borders to a Cell
# 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 # 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')

Setting the Background Color of a Cell
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')

TODO: Things Left to Document
`- Panes -- separate views which are always in view

  • Border Colors (documented above, but not taking effect as it should)
  • Border Widths (document above, but not working as expected)
  • Protection
  • Row Styles
  • Zoom / Manification
  • WS Props?
    Source Code for reference available at: https://secure.simplistix.co.uk/svn/xlwt/trunk/xlwt/`
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市毒坛,隨后出現(xiàn)的幾起案子仍秤,更是在濱河造成了極大的恐慌靴患,老刑警劉巖源哩,帶你破解...
    沈念sama閱讀 206,839評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件厘线,死亡現(xiàn)場離奇詭異鸳惯,居然都是意外死亡商蕴,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評論 2 382
  • 文/潘曉璐 我一進店門芝发,熙熙樓的掌柜王于貴愁眉苦臉地迎上來绪商,“玉大人,你說我怎么就攤上這事辅鲸「裼簦” “怎么了?”我有些...
    開封第一講書人閱讀 153,116評論 0 344
  • 文/不壞的土叔 我叫張陵独悴,是天一觀的道長例书。 經(jīng)常有香客問我,道長刻炒,這世上最難降的妖魔是什么决采? 我笑而不...
    開封第一講書人閱讀 55,371評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮坟奥,結果婚禮上织狐,老公的妹妹穿的比我還像新娘。我一直安慰自己筏勒,他們只是感情好移迫,可當我...
    茶點故事閱讀 64,384評論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著管行,像睡著了一般厨埋。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上捐顷,一...
    開封第一講書人閱讀 49,111評論 1 285
  • 那天荡陷,我揣著相機與錄音雨效,去河邊找鬼。 笑死废赞,一個胖子當著我的面吹牛徽龟,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播唉地,決...
    沈念sama閱讀 38,416評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼据悔,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了耘沼?” 一聲冷哼從身側響起极颓,我...
    開封第一講書人閱讀 37,053評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎群嗤,沒想到半個月后菠隆,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,558評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡狂秘,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,007評論 2 325
  • 正文 我和宋清朗相戀三年骇径,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片者春。...
    茶點故事閱讀 38,117評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡既峡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出碧查,到底是詐尸還是另有隱情,我是刑警寧澤校仑,帶...
    沈念sama閱讀 33,756評論 4 324
  • 正文 年R本政府宣布忠售,位于F島的核電站,受9級特大地震影響迄沫,放射性物質(zhì)發(fā)生泄漏稻扬。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,324評論 3 307
  • 文/蒙蒙 一羊瘩、第九天 我趴在偏房一處隱蔽的房頂上張望泰佳。 院中可真熱鬧,春花似錦尘吗、人聲如沸逝她。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽黔宛。三九已至,卻和暖如春擒贸,著一層夾襖步出監(jiān)牢的瞬間臀晃,已是汗流浹背觉渴。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留徽惋,地道東北人案淋。 一個月前我還...
    沈念sama閱讀 45,578評論 2 355
  • 正文 我出身青樓,卻偏偏與公主長得像险绘,于是被迫代替她去往敵國和親踢京。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 42,877評論 2 345

推薦閱讀更多精彩內(nèi)容