xlwt设置Excel单元格style样式(xlwt设置单元格格式)

网友投稿 1230 2022-08-28


xlwt设置Excel单元格style样式(xlwt设置单元格格式)

python xlwt 设置单元格样式

1、使用xlwt中的Alignment来设置单元格的对齐方式

其中horz代表水平对齐方式,vert代表垂直对齐方式。

VERT_TOP = 0x00 上端对齐VERT_CENTER = 0x01 居中对齐(垂直方向上)VERT_BOTTOM = 0x02 低端对齐HORZ_LEFT = 0x01 左端对齐HORZ_CENTER = 0x02 居中对齐(水平方向上)HORZ_RIGHT = 0x03 右端对齐

import xlwt def run(): wk = xlwt.Workbook() # 新建一个表格 sheet = wk.add_sheet('sheet1') # 给表格新建一个名为 sheer1 的工作簿 style = xlwt.XFStyle() # 创建一个样式对象,初始化样式 al = xlwt.Alignment() al.horz = 0x02 # 设置水平居中 al.vert = 0x01 # 设置垂直居中 style.alignment = al sheet.write(0, 0, '文本居中', style) wk.save('Test.xls') run()

import xlwt workbook = xlwt.Workbook(encoding = 'ascii')worksheet = workbook.add_sheet('My Worksheet')style = xlwt.XFStyle() # 初始化样式font = xlwt.Font() # 为样式创建字体font.name = 'Times New Roman'font.bold = True # 黑体font.underline = True # 下划线font.italic = True # 斜体字style.font = font # 设定样式worksheet.write(0, 0, 'Unformatted value') # 不带样式的写入 worksheet.write(1, 0, 'Formatted value', style) # 带样式的写入 workbook.save('formatting.xls') # 保存文件

2、设置宽度

import xlwt workbook = xlwt.Workbook()worksheet = workbook.add_sheet('My Sheet')worksheet.write(0, 0,'My Cell Contents') # 设置单元格宽度worksheet.col(0).width = 3333workbook.save('cell_width.xls')

3、添加超链接

import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('My Sheet')worksheet.write(0, 0, xlwt.Formula('HYPERLINK("# Outputs the text "Google" linking to xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('My Sheet')alignment = xlwt.Alignment() # Create Alignmentalignment.horz = xlwt.Alignment.HORZ_CENTER # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTEDalignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTEDstyle = xlwt.XFStyle() # Create Stylestyle.alignment = alignment # Add Alignment to Styleworksheet.write(0, 0, 'Cell Contents', style)workbook.save('Excel_Workbook.xls')

5、添加边框

import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('My Sheet')borders = xlwt.Borders() # Create Bordersborders.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.DASHEDborders.top = xlwt.Borders.DASHEDborders.bottom = xlwt.Borders.DASHEDborders.left_colour = 0x40borders.right_colour = 0x40borders.top_colour = 0x40borders.bottom_colour = 0x40style = xlwt.XFStyle() # Create Stylestyle.borders = borders # Add Borders to Styleworksheet.write(0, 0, 'Cell Contents', style)workbook.save('Excel_Workbook.xls')

6、设置背景颜色

import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('My Sheet')pattern = xlwt.Pattern() # Create the Patternpattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12pattern.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 Patternstyle.pattern = pattern # Add Pattern to Styleworksheet.write(0, 0, 'Cell Contents', style)workbook.save('Excel_Workbook.xls')

7、实际应用带详细注释

Xlwt设置style样式

使用xlwt保存内容到excel的时候,可能有些格式需求,比如想要文字居中,想要单元格字体加粗,想要改变列宽等等以下记录的是使用xlwt的style样式实现几个常用的单元格样式设置

import xlwt# 为样式创建字体def get_font(): font = xlwt.Font() # 字体类型 font.name = 'name Times New Roman' # 字体颜色 font.colour_index = 4 # 字体大小,11为字号,20为衡量单位 font.height = 20 * 11 # 字体加粗 font.bold = False # 下划线 font.underline = True # 斜体字 font.italic = True return font# 设置单元格对齐方式def get_alignment(): alignment = xlwt.Alignment() # 0x01(左端对齐)、0x02(水平方向上居中对齐)、0x03(右端对齐) alignment.horz = 0x02 # 0x00(上端对齐)、 0x01(垂直方向上居中对齐)、0x02(底端对齐) alignment.vert = 0x01 # 设置自动换行 alignment.wrap = 1 return alignment# 设置边框def get_borders(): borders = xlwt.Borders() # 细实线:1,小粗实线:2,细虚线:3,中细虚线:4,大粗实线:5,双线:6,细点虚线:7 # 大粗虚线:8,细点划线:9,粗点划线:10,细双点划线:11,粗双点划线:12,斜点划线:13 borders.left = 1 borders.right = 2 borders.top = 3 borders.bottom = 4 borders.left_colour = 1 borders.right_colour = 1 borders.top_colour = 1 borders.bottom_colour = 1 return borders# 主程序def main(): # 创建一个xlwt对象 excel = xlwt.Workbook(encoding='utf-8') # 在excel文件中新建工作表 sheet = excel.add_sheet('格式测试') # 设置列宽,一个中文等于两个英文等于两个字符,11为字符数,256为衡量单位 sheet.write(0, 0, "Python") sheet.col(0).width = 25 * 256 # 初始化样式 style_font = xlwt.XFStyle() style_alignment = xlwt.XFStyle() style_borders = xlwt.XFStyle() # 获取设置字体格式 style_font.font = get_font() # 获取设置对齐格式 style_alignment.alignment = get_alignment() # 获取设置边框格式 style_borders.borders = get_borders() # 将三种样式定义的格式作用与三个单元格 sheet.write(1, 0, '字体样式', style_font) sheet.write(1, 2, '对齐方式', style_alignment) sheet.write(1, 4, '边框样式', style_borders) excel.save('../Data/excel/style.xls')# 主程序入口if __name__ == "__main__": main()

去期待陌生,去拥抱惊喜。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:CSV文件解析(csv文件解析 c语言)
下一篇:Spring Cloud Feign实现文件上传下载的示例代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~