zoukankan      html  css  js  c++  java
  • 24.python中xlwt模块用法详解

    1.创建并保存一个excel

    创建一个工作簿,设置编码格式为“utf-8”,默认格式是ASCII,为了方便写入中文,一般都要设置成UTF-8

    import xlwt
    wb = xlwt.Workbook(encoding="utf-8")  # 创建一个工作薄
    sheet = wb.add_sheet('文件阅读记录表')  # 创建一个工作表
    wb.save(
    "xlwt_test.xls")  # 保存excle文件

    2.设置列宽度

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

    所以我们在设置列宽时可以用如下方法:width = 256 * 20    256为衡量单位,20表示20个字符宽度

    # 设置单元格的宽度
    # 循环设置第一列至第五列的列宽
    for c in range(5):
         sheet.col(c).width=256*20    # 设置列宽值为20个字符宽度

    3.设置文本居中

    使用xlwt中的Alignment来设置单元格的对齐方式,其中horz代表水平对齐方式,vert代表垂直对齐方式。
    VERT_TOP = 0x00 上端对齐
    VERT_CENTER = 0x01 居中对齐(垂直方向上)
    VERT_BOTTOM = 0x02 低端对齐
    HORZ_LEFT = 0x01 左端对齐
    HORZ_CENTER = 0x02 居中对齐(水平方向上)
    HORZ_RIGHT = 0x03 右端对齐

    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')

    4.设置字体样式

    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') # 保存文件

    5.添加链接(xwl亲测可用)

    import xlwt
    file='霸王别姬.pdf'
    link=r"E:.otherpyworkstudy11day26
    eadRecord{}".format(file)
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet('My Sheet')
    worksheet.write(0, 0, xlwt.Formula('HYPERLINK("{}";"{}")'.format(link,file))) # Formula后的括号中其实就是一个字符串,所以用format来占用字符
    workbook.save('Excel_Workbook.xls')

    6.添加边框

    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')

    7.设置背景色颜色

    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')
  • 相关阅读:
    使用Fiddler和雷电模拟器抓取安卓https请求
    Robot Framework -- 安装接口测试库
    Robot Framework --为了进行Web测试,安装SeleniumLibrary外部库
    robotframework+python3+eclipse+RED自动化测试框架初学笔记
    LeetCode Weekly Contest 27
    LeetCode Weekly Contest 26
    京东4.7实习笔试题
    牛客网模拟笔试 2
    4.1几道最近的题目
    网易3.25实习笔试
  • 原文地址:https://www.cnblogs.com/ubuntu1987/p/11926021.html
Copyright © 2011-2022 走看看