zoukankan      html  css  js  c++  java
  • Python_xlwt模块介绍

    简介:
    xlwt是Python中往excel中写入数据的模块

    1. 创建Book工作簿(即excel工作簿)
    1 import xlwt
    2 workbook = xlwt.Workbook(encoding = 'utf-8')
    3     # 创建一个workbook并设置编码形式


    2. 添加sheet工作表
    1 worksheet = workbook.add_sheet('My Worksheet')
    2     # 创建一个worksheet
    
    
    
    
    3. 向工作表中添加数据并保存
    
    1 worksheet.write(1,0, label = 'this is test')
    2     # 参数对应 行, 列, 值
    3 
    4 workbook.save('Excel_test.xls')
    5     # 保存

     

     

    4. 格式设置

    4.1 字体设置
     1 import xlwt
     2  
     3 workbook = xlwt.Workbook(encoding = 'ascii')
     4 
     5 worksheet = workbook.add_sheet('My Worksheet')
     6      # 添加名为My Worksheet的工作表
     7 
     8  style = xlwt.XFStyle() 
     9      # 初始化样式
    10 
    11 font = xlwt.Font() 
    12      # 为样式创建字体
    13 
    14 font.name = 'Times New Roman'
    15 
    16 font.bold = True 
    17      # 黑体
    18 
    19 font.underline = True 
    20     # 下划线
    21 
    22 font.italic = True 
    23     # 斜体字
    24 
    25 style.font = font 
    26     # 设定样式
    27 
    28 worksheet.write(0, 0, 'Unformatted value')     
    29     # 不带样式的写入
    30 
    31 worksheet.write(1, 0, 'Formatted value', style) 
    32     # 带样式的写入
    33 
    34 workbook.save('formatting.xls') 
    35     # 保存文件
    
    

    4.2 设置单元格宽度和高度:

     1 import xlwt
     2 
     3 workbook = xlwt.Workbook()
     4 worksheet = workbook.add_sheet('My Sheet')
     5 worksheet.write(0, 0,'My Cell Contents')
     6 
     7 
     8 worksheet.col(0).width = 3333
     9     # 设置单元格宽度
    10 worksheet.col(0).height = 3333
    11     # 设置单元格宽度
    12 
    13 workbook.save('cell_width.xls')
    
    
  • 相关阅读:
    451. Sort Characters By Frequency
    424. Longest Repeating Character Replacement
    68. Text Justification
    44. Wildcard Matching
    160. Intersection of Two Linked Lists
    24. Swap Nodes in Pairs
    93. 递归实现组合型枚举
    98. 分形之城
    97. 约数之和
    96. 奇怪的汉诺塔
  • 原文地址:https://www.cnblogs.com/carreyBlog/p/11617952.html
Copyright © 2011-2022 走看看