python xlwings设置单元格格式,python的xlwt库能生成excel的哪种格式
很多人的印象是编程很枯燥,但是有很多有趣的。今天,我们将改变单元格格式,使表格赏心悦目。
使用xlwt中的Alignment设置单元格的对齐方式,其中horz代表水平对齐,vert代表垂直对齐。
VERT_TOP=0x00顶部对齐
VERT_CENTER=0x01中心对齐(垂直方向)
VERT_BOTTOM=0x02低端对齐
HORZ_LEFT=0x01左对齐
HORZ_CENTER=0x02中心对齐(水平)
HORZ_RIGHT=0x03右端对齐
导入xlwt
workbook=xlwt。工作簿(编码=ascii )
worksheet=workbook.add_sheet(我的工作表)
Style=xlwt。XFStyle() #初始化样式
Font=xlwt。Font() #为样式创建字体
font.name=Times New Roman
Font.bold=True # bold
Font.underline=True # underline
Font.italic=True #斜体
设置样式
Worksheet.write (0,0,无格式值)#不带样式写入
Worksheet.write (1,0,格式化值,style) #使用样式写入
workbook . Save( formatting . xls )#保存文件
>
设置宽度
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet(‘My Sheet‘)
worksheet.write(0, 0,‘My Cell Contents‘)
# 设置单元格宽度
worksheet.col(0).width = 3333
workbook.save(‘cell_width.xls‘)
添加超链接
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‘)
对其方式
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‘)
添加边框
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet(‘My Sheet‘)
borders = xlwt.Borders() # Create Borders
borders.left = xlwt.Borders.DASHED
DASHED虚线
NO_LINE没有
THIN实线
# 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‘)
设置背景色颜色
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‘)
以上就是Python中xlwt设置单元格格式的方法。更多Python学习推荐:PyThon学习网教学中心。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。