标签:vertica -o styles image 生成 style oss order Oz
实现一个函数,生成一个excel内容如下:示例:
#coding=utf-8
from openpyxl.styles import Border,S ide,PatternFill,Font,Alignment,colors
from openpyxl import Workbook
def set_xlsx(filepath):
#新建一个excel表格对象wb = Workbook()
#获取excel的第一个sheetws = wb.active
#添加一行内容,分别添加到A1 B1 C1 D1 E1单元格内,append添加的是一个列表ws.append([u"姓名",u"性别",u"学号",u"爱好",u"年龄"])
#添加第2到第6行数据
for i in range(5):
ws.append([u"张三",u"男",str(i) + "001",u"打蓝球",str(18+i)])
#定义字体格式
font = Font(name=u"宋体",color=colors.RED,size=16,bold=True)
#定义边框线的格式thin = Side(border_style = "thin",color = colors.BLACK)
#定义边框四个边的格式border = Border(top = thin,left = thin,right = thin,bottom = thin)
#定义水平、垂直对其的方式al = Alignment(horizontal = "center",vertical = "center")
#定义单元格背景填充格式fill = PatternFill("solid",fgColor = "DADDBA")
#设置首行每个单元的格式
for cell in ws.rows[0]:#ws.rows[0] 返回第一行的所有单元格
cell.fill = fill
cell.border = border
cell.font = font
cell.alignment = al
font1 = Font(name = u"微软雅黑",color = colors.BLACK,size = 14,bold = True)
fill2 = PatternFill("solid",fgColor = "A2CD5A")
#设置第2到第6行每个单元的格式
for i in range(1,6):
for cell in ws.rows[i]:
cell.font = font1
cell.fill = fill2
cell.border = border
cell.alignment = al
#保存excel文件
wb.save(filepath)
set_xlsx("test013.xlsx")
标签:vertica -o styles image 生成 style oss order Oz
原文地址:http://blog.51cto.com/13496943/2134205