(二)xlwt模块详解--设置列宽、行高

第二篇博客是关于设置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')

worksheet.write(0, 0, 'My cell Contents')

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工作表;指定单元格写入内容;利用索引找出列索引,直接写宽度。

           效果图:

(二)xlwt模块详解--设置列宽、行高_第1张图片

怎么样?是不是很简单!

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')  # Create workbook
sheet = workbook.add_sheet('My sheet1')  # Create sheet
first_col = sheet.col(0)
sec_col = sheet.col(1)


first_col.width = 256 * 20  # Set the column width
tall_style = xlwt.easyxf('font:height 720')  # 36pt
first_row = sheet.row(0)
first_row.set_style(tall_style)

workbook.save('E:\\test\\xls_xlwt\Excel_row_height.xls')

效果图如下:

(二)xlwt模块详解--设置列宽、行高_第2张图片

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

 

你可能感兴趣的:(The,approach,of,data,analysis)