• 技术文章 >Python技术 >Python基础教程

    Python实用:用xlwt设置表格列宽和行高

    2020-11-05 15:39:18原创8236

    为了表格的美观和大方,我们需要改动列宽和行高。今天就为大家来的Python中xlwt的设置方法。

    一:先创建一个excel


    1

    2

    3

    4

    5

    6

    7

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;">‘‘‘

    Created on 2015-11-19

    @author: Administrator

    ‘‘‘

    import xlwt

    book = xlwt.Workbook(encoding=‘utf-8‘)

    sheet = book.add_sheet(‘sheet1‘)<br></span></p>


    二、设置列宽度


    xlwt中列宽的值表示方法:默认字体0的1/256为衡量单位。xlwt创建时使用的默认宽度为2960,既11个字符0的宽度

    所以我们在设置列宽时可以用如下方法:

    width = 256 * 20 256为衡量单位,20表示20个字符宽度

    那接下来完成我们的程序


    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;">#coding:utf-8

    ‘‘‘

    Created on 2015-11-19

    @author: Administrator

    ‘‘‘

    import xlwt

    book = xlwt.Workbook(encoding=‘utf-8‘)

    sheet = book.add_sheet(‘sheet1‘)

    first_col=sheet.col(0)       #xlwt中是行和列都是从0开始计算的

    sec_col=sheet.col(1)

     

    first_col.width=256*20  

     

     

    book.save(‘width.xls‘)<br></span></p>

    效果就如下:



    三、行高


    行宽是在单元格的样式中设置的,你可以通过自动换行通过输入文字的多少来确定行高

    一般如下方法:


    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;">#coding:utf-8

    ‘‘‘

    Created on 2015-11-19

    @author: Administrator

    ‘‘‘

    import xlwt

    book = xlwt.Workbook(encoding=‘utf-8‘)

    sheet = book.add_sheet(‘sheet1‘)

    first_col=sheet.col(0)

    sec_col=sheet.col(1)

     

    first_col.width=256*20

    tall_style = xlwt.easyxf(‘font:height 720;‘) # 36pt,类型小初的字号

    first_row = sheet.row(0)

    first_row.set_style(tall_style)

     

     

    book.save(‘width.xls‘)<br></span></p>


    效果如下:


    四、其它

    在xlwt中没有特定的函数来设置默认的列宽及行高。


    以上就是用xlwt设置表格列宽和行高的方法。更多Python学习推荐:PyThon学习网教学中心

    专题推荐:xlwt设置列宽和行高;python
    上一篇:Python中xlwt如何访问工作表 下一篇:详解python filter函数的用法及使用

    相关文章推荐

    • python中xlwt是什么• Python中如何用xlwt制作表格• Python中如何安装xlwt• Python中xlwt如何设置单元格格式• Python实用之xlwt单元格中加超链接• Python中xlwt如何访问工作表

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网