python xlwt写excel格式控制 颜色、模式、编码、背景色

此页面是否是列表页或首页?未找到合适正文内容。

python xlwt写excel格式控制 颜色、模式、编码、背景色

标签:httppackhtmlodimediumfirsthtmblankhex

关于写excel的格式控制,比如颜色等等

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

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 Exampleimport xlwtworkbook = 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 Cellimport xlwtworkbook = xlwt.Workbook(encoding = ‘ascii‘)worksheet = workbook.add_sheet(‘My Worksheet‘)font = xlwt.Font() # Create the Fontfont.name = ‘Times New Roman‘font.bold = Truefont.underline = Truefont.italic = Truestyle = xlwt.XFStyle() # Create the Stylestyle.font = font # Apply the Font to the Styleworksheet.write(0, 0, label = ‘Unformatted value‘)worksheet.write(1, 0, label = ‘Formatted value‘, style) # Apply the Style to the Cellworkbook.save(‘Excel_Workbook.xls‘)

Attributes of the Font Objectfont.bold = True # May be: True, Falsefont.italic = True # May be: True, Falsefont.struck_out = True # May be: True, Falsefont.underline = xlwt.Font.UNDERLINE_SINGLE # May be: UNDERLINE_NONE, UNDERLINE_SINGLE, UNDERLINE_SINGLE_ACC, UNDERLINE_DOUBLE, UNDERLINE_DOUBLE_ACCfont.escapement = xlwt.Font.ESCAPEMENT_SUPERSCRIPT # May be: ESCAPEMENT_NONE, ESCAPEMENT_SUPERSCRIPT, ESCAPEMENT_SUBSCRIPTfont.family = xlwt.Font.FAMILY_ROMAN # May be: FAMILY_NONE, FAMILY_ROMAN, FAMILY_SWISS, FAMILY_MODERN, FAMILY_SCRIPT, FAMILY_DECORATIVE

作者: 库巴司机

为您推荐

返回顶部