def set_style(name, height, bold=False, center=True):
style = xlwt.XFStyle() # ?????
font = xlwt.Font() # ???????
font.name = name # 'Times New Roman'
font.bold = False
font.color_index = 0
font.height = height
# borders= xlwt.Borders()
# borders.left= 6
# borders.right= 6
# borders.top= 6
# borders.bottom= 6
style.font = font
# style.borders = borders
alignment = xlwt.Alignment()
alignment.horz = xlwt.Alignment.HORZ_CENTER
if center == True:
style.alignment = alignment
return style
python类Font()的实例源码
def set_excel_style(name, height, bold=False):
"""Set excel style.
"""
style = xlwt.XFStyle() # ?????
font = xlwt.Font() # ???????
font.name = name # ??'Times New Roman'
font.bold = bold
font.color_index = 4
font.height = height
if bold:
borders = xlwt.Borders()
borders.left = 6
borders.right = 6
borders.top = 6
borders.bottom = 6
style.borders = borders
style.font = font
return style
def create_simple_xls(self, **kw):
font0 = xlwt.Font()
font0.name = 'Times New Roman'
font0.colour_index = 2
font0.bold = True
style0 = xlwt.XFStyle()
style0.font = font0
style1 = xlwt.XFStyle()
style1.num_format_str = 'D-MMM-YY'
wb = xlwt.Workbook(**kw)
ws = wb.add_sheet('A Test Sheet')
ws.write(0, 0, 'Test', style0)
ws.write(1, 0, datetime(2010, 12, 5), style1)
ws.write(2, 0, 1)
ws.write(2, 1, 1)
ws.write(2, 2, xlwt.Formula("A3+B3"))
return wb, ws
def setup(self):
if self.report.ftype == REPORT.XLS:
font_h = xlwt.Font()
font_h.bold = True
style_h = xlwt.XFStyle()
style_h.font = font_h
self.xls_styles = {
'datetime': xlwt.easyxf(num_format_str='D/M/YY h:mm'),
'date': xlwt.easyxf(num_format_str='D/M/YY'),
'time': xlwt.easyxf(num_format_str='h:mm'),
'default': xlwt.Style.default_style,
'bold': style_h
}
self.wb = xlwt.Workbook()
self.ws = self.wb.add_sheet('Data')
if self.make_sub_reports:
self.section_ws = self.wb.add_sheet('Section')
def create_simple_xls(self, **kw):
font0 = xlwt.Font()
font0.name = 'Times New Roman'
font0.colour_index = 2
font0.bold = True
style0 = xlwt.XFStyle()
style0.font = font0
style1 = xlwt.XFStyle()
style1.num_format_str = 'D-MMM-YY'
wb = xlwt.Workbook(**kw)
ws = wb.add_sheet('A Test Sheet')
ws.write(0, 0, 'Test', style0)
ws.write(1, 0, datetime(2010, 12, 5), style1)
ws.write(2, 0, 1)
ws.write(2, 1, 1)
ws.write(2, 2, xlwt.Formula("A3+B3"))
return wb, ws
def set_style(name, height, bold=False):
style = xlwt.XFStyle() # ?????
font = xlwt.Font() # ???????
font.name = name # 'Times New Roman'
font.bold = bold
font.color_index = 4
font.height = height
# borders= xlwt.Borders()
# borders.left= 6
# borders.right= 6
# borders.top= 6
# borders.bottom= 6
style.font = font
# style.borders = borders
return style
def get_title(self):
# ????
font = xlwt.Font()
# font.name = "Arial" #????
font.colour_index = 1 # ?????
font.bold = True # ????
font.height = 20 * 11 # ??????????????????????
# ?????
patterni = xlwt.Pattern()
patterni.pattern = xlwt.Pattern.SOLID_PATTERN
patterni.pattern_fore_colour = 24 #???????
# ????
style = xlwt.XFStyle()
style.font = font
style.pattern = patterni
for r,title in zip(range(12),["??","??","????","??","????","????","????","????","?????",
"????","????","????"]):
self.sheet.write(0,r,title,style)
for x in [0,3,4,5,6,7,9]:
self.sheet.col(x).width = 256*8
self.sheet.col(1).width = 250*16
self.sheet.col(2).width = 256*30
self.sheet.col(8).width = 256*40
self.sheet.col(10).width = 256*20
self.sheet.col(11).width = 256*50
# ???????i??????????????
def generate_xls(urls, keys, result,filename):
"""?????excel??"""
xls_file = xlwt.Workbook()
sheet = xls_file.add_sheet(unicode(filename), cell_overwrite_ok=True)
font0 = xlwt.Font()
font0.name = 'Times New Roman'
font0.colour_index = 2
font0.bold = True
style0 = xlwt.XFStyle()
style0.font = font0
row = 0
sheet.col(0).width = 256 * 20
sheet.col(1).width = 256 * 30
sheet.col(2).width = 256 * 20
sheet.write(0, 0, u"??", style0)
sheet.write(0, 1, u"???", style0)
sheet.write(0, 2, u"??", style0)
if urls != None and keys != None and result != None:
for url, key, res in zip(urls, keys, result):
row += 1
sheet.write(row, 0, url.decode("utf-8"))
sheet.write(row, 1, key.decode("utf-8"))
sheet.write(row, 2, res.decode("utf-8"))
fpath=os.path.join(os.path.dirname(sys.argv[0]),u"{}.xls".format(unicode(filename))) #??????
print u"[??]???????{}".format(fpath)
xls_file.save(fpath)
def set_style(name, height,bold=False):
style = xlwt.XFStyle() # ?????
font = xlwt.Font() # ???????
font.name = name # 'Times New Roman'
font.bold = bold
font.color_index = 4
font.height = height
return style
def set_style(name, height,bold=False):
style = xlwt.XFStyle() # ?????
font = xlwt.Font() # ???????
font.name = name # 'Times New Roman'
font.bold = bold
font.color_index = 4
font.height = height
return style
def set_style(font_name,height,bold=False):
style = xlwt.XFStyle() #?????
font = xlwt.Font() #??
font.name = font_name
font.bold = bold
font.colour_index = 4
font.height = height
style.font = font
return style
def get_default_style(self):
"""
* Colour index
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... sty
* Borders
borders.left, borders.right, borders.top, borders.bottom
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 = xlwt.Borders()
borders.left = xlwt.Borders.THIN
borders.right = xlwt.Borders.THIN
borders.top = xlwt.Borders.THIN
borders.bottom = xlwt.Borders.THIN
borders.left_colour = 0x00
borders.right_colour = 0x00
borders.top_colour = 0x00
borders.bottom_colour = 0x00
style.borders = borders
* Fonts
style.font = xlwt.Font()
style.font.height = 8 * 20
style.font.colour_index = 22
* Alignment
style.alignment = xlwt.Alignment()
style.alignment.horz = xlwt.Alignment.HORZ_LEFT
style.alignment.vert = xlwt.Alignment.VERT_CENTER
* Pattern
May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
style.pattern = xlwt.Pattern()
style.pattern.pattern = xlwt.Pattern.SOLID_PATTERN
style.pattern.pattern_fore_colour = 23
"""
style = xlwt.XFStyle()
return style