(二)xlwt模块详解

您所在的位置:网站首页 excel设置行宽列宽 (二)xlwt模块详解

(二)xlwt模块详解

2024-07-10 09:49| 来源: 网络整理| 查看: 265

第二篇博客是关于设置Excel中的列宽和行高。

废话不多说,直接上代码看效果!

1.设置列宽

           xlwt中列宽的值表示方法:默认字体0的1/256为衡量单位。其创建时使用的默认宽度为2960,即11个字符0的宽度。所以我们在设置列宽时可以使用如下办法:

           width = 256 * 20    # 256为衡量单位,20表示20个字符宽度            那接下来完成我们的程序:

#!/usr/bin/env python3.6 # encoding: utf-8 ''' @author: Leo @contact: @software: PyCharm @file: excel_width.py @time: 2018/10/15 下午 05:39 @desc: ''' import xlwt workbook = xlwt.Workbook() # 创建工作簿 worksheet = workbook.add_sheet('My sheet1') # 增加sheet页 worksheet.write(0, 0, 'My cell Contents') # 命名上面新建的sheet页 worksheet.col(0).width = 256 * 20 # Set the column width 设置第一列列宽 worksheet.col(2).width = 8888 # Set the column width 设置第三列列宽 workbook.save('E:\\test\\xls_xlwt\Excel_cell_width.xls') # 保存文件

创建workbook对象、sheet工作表;指定单元格写入内容;利用索引找出列索引,直接写宽度。

           效果图:

怎么样?是不是很简单!

2.设置行高 #!/usr/bin/env python3.6 # encoding: utf-8 ''' Author: Leo Contact: [email protected] Software: PyCharm File: excel_height.py Time: 2018/10/30 上午 08:38 Desc: ''' import xlwt workbook = xlwt.Workbook(encoding='utf-8') # 创建工作簿 sheet = workbook.add_sheet('My sheet1') # 创建sheet first_col = sheet.col(0) # 获取第一列 sec_col = sheet.col(1) # 获取第二列 first_col.width = 256 * 20 # 设置第一列列宽 tall_style = xlwt.easyxf('font:height 720') # 设置行高 first_row = sheet.row(0) # 获取sheet页的第一行 first_row.set_style(tall_style) # 给第一行设置tall_style样式,也就是行高 workbook.save('E:\\test\\xls_xlwt\Excel_row_height.xls')

效果图如下:

今日就先更新到这里~,明天更新内容为合并单元格部分内容。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3