只适用于 Excel 2003 及以下版本,进行写操作
# 创建一个 excel, 设置编码
workbook = xlwt.Workbook(encoding='utf-8')
# 创建一个 sheet
sheet = workbook.add_sheet('My Worksheet')
# 写入单元格 (对应为:行,列,内容)
sheet.write(0, 0, label='this is test')
# 设置样式
# 初始化样式
style = xlwt.XFStyle()
# 创建字体
font = xlwt.Font()
font.name = 'Times New Roman'
# 设置黑体
font.bold = True
# 设置下划线
font.underline = True
# 设置斜体字
font.italic = True
# 设定样式
style.font = font
# 不带样式的写入单元格
sheet.write(1, 0, 'Unformatted value')
# 带样式的写入单元格
sheet.write(2, 0, 'Formatted value', style)
# 设置单元格 列宽 (默认字体0的1/256为衡量单位,256为衡量单位,20表示20个字符宽度)
sheet.col(0).width = 256 * 20
# 设置单元格 行高
high_style = xlwt.easyxf('font:height 512')
worksheet.row(0).set_style(high_style)
# 输入一个日期到单元格
style = xlwt.XFStyle()
# 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
style.num_format_str = 'M/D/YY'
worksheet.write(3, 0, datetime.datetime.now(), style)
# 单元格添加一个公式
# B1 为 5
worksheet.write(0, 1, 5)
# C1 为 2
worksheet.write(0, 2, 2)
# Should output "10"
worksheet.write(0, 3, xlwt.Formula('B1*C1'))
# Should output "7"
worksheet.write(0, 4, xlwt.Formula('SUM(B1,C1)'))
# 单元格添加一个超链接
# Outputs the text "Google" linking to http://www.google.com
worksheet.write(4, 0, xlwt.Formula('HYPERLINK("http://www.google.com";"Google")'))
# 合并列和行
# 合并第6行的第0列到第3列, Merges row 0's columns 0 through 3.
worksheet.write_merge(5, 5, 0, 3, 'First Merge')
# Create Font
font = xlwt.Font()
# Set font to Bold
font.bold = True
# Create Style
style = xlwt.XFStyle()
# Add Bold Font to Style
style.font = font
# 合并第7行到第8行的第0列到第3列, Merges row 1 through 2's columns 0 through 3.
worksheet.write_merge(6, 7, 0, 3, 'Second Merge', style)
# 设置单元格内容的对其方式
# 创建对其方式 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
# May be:
# VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
alignment.vert = xlwt.Alignment.VERT_CENTER
# Create Style
style = xlwt.XFStyle()
# 将对其方式添加到样式中, Add Alignment to Style
style.alignment = alignment
worksheet.write(8, 0, 'Cell Contents', style)
# 单元格添加边框
borders = xlwt.Borders() # Create Borders
# 虚线: DASHED, 没有边框: NO_LINE, 实线: THIN
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
# Create Style
style = xlwt.XFStyle()
# Add Borders to Style
style.borders = borders
worksheet.write(9, 0, 'Cell Contents', style)
# 单元格设置背景色
# Create the Pattern
pattern = xlwt.Pattern()
# May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
# 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
# Create the Pattern
style = xlwt.XFStyle()
# Add Pattern to Style
style.pattern = pattern
worksheet.write(10, 0, 'Cell Contents', style)
# 保存
workbook.save('Excel_test.xls')