1. 模块简介

1)xlrd是可用于对excel表格进行写操作的(不支持读操作,读操作需要xlrd模块实现)
 2)仅支持 xls 格式的excel表格(xlwt支持Excel versions 95 to 2003;xlsx格式是2007及以上版本的excel的文件扩展名)
 3)模块安装方式:pip3 install xlwt
 4)模块导入方式: import xlwt

2. 模块的使用

2.1 向xls文件中写入内容

方法介绍

# 导入xlwt模块
import xlwt# 创建一个workbook对象,就相当于创建了一个Excel文件
book = xlwt.Workbook(encoding='utf-8',style_compression=0)  # encoding:设置编码,可写中文;style_compression:是否压缩,不常用# 创建一个sheet对象,相当于创建一个sheet页
sheet = book.add_sheet('test_sheet',cell_overwrite_ok=True) # cell_overwrite_ok:是否可以覆盖单元格,默认为False# 向sheet页中添加数据
text = '字串测试'
sheet.write(1,2,'EnglishTest')  # 第1行第2列,写入'EnglishTest';从第0行开始计数
sheet.write(2,2,'中文测试')
sheet.write(3,2,text)# 将以上内容保存到指定的文件中
book.save('blog.xls')   # 在字符串前加r,声明为raw字符串,这样就不会处理其中的转义了

执行结果

2.2 设置写入文件的格式

2.2.1 字体设置(font)

1)语法描述

# ...代码掠过...
# 1. 初始化样式
style = xlwt.XFStyle()# 2. 为样式创建字体(font)
font = xlwt.Font()# 3. 指定字体的具体属性(仅列出常用属性)
font.name = 'Times New Roman'   # 指定字体
font.height = 300   # 和excel字体大小比例是1:20
font.bold = True    # 字体是否加粗
font.underline = True   # 字体是否下划线
font.struck_out = True  # 字体是否有横线
font.italic = True  # 是否斜体字
font.colour_index = 4   # 字体颜色
# [...其他属性掠过...]# 4. 设定字体样式
style.font = font# 5. 带样式将内容写入Excel
sheet.write(1,2,'EnglishTest',style)  # style代表上面定义的样式
# ...代码掠过...

2)代码示例说明指定字体各属性

# 导入xlwt模块
import xlwt# 创建一个workbook对象,就相当于创建了一个Excel文件
book = xlwt.Workbook(encoding='utf-8',style_compression=0)  # encoding:设置编码,可写中文;style_compression:是否压缩,不常用
# 创建一个sheet对象,相当于创建一个sheet页
sheet = book.add_sheet('test_sheet',cell_overwrite_ok=True) # cell_overwrite_ok:是否可以覆盖单元格,默认为False# 初始化样式
style = xlwt.XFStyle()# 1. 为样式创建字体(font)
font = xlwt.Font()
# 指定字体的具体属性(仅列出常用属性)
font.name = 'Times New Roman'   # 指定字体
font.height = 300   # 和excel字体大小比例是1:20
font.bold = True    # 字体是否加粗
font.underline = True   # 字体是否下划线
font.struck_out = True  # 字体是否有横线
font.italic = True  # 是否斜体字
font.colour_index = 4   # 字体颜色# 设定字体样式
style.font = font# 向sheet页中添加数据
text = '字串测试'
sheet.write(1,2,'EnglishTest',style)  # 第1行第2列,写入'EnglishTest';从第0行开始计数;带上面定义的style样式写入
sheet.write(2,2,'中文测试',style)
sheet.write(3,2,text,style)# 将以上内容保存到指定的文件中
book.save('blog.xls')   # 在字符串前加r,声明为raw字符串,这样就不会处理其中的转义了

3)执行结果

4)补充字体颜色对照图

字体和单元格背景颜色的对应关系是一样的

2.2.2 背景颜色设置(pattern)

1)语法描述

# ...代码掠过...
# 1. 初始化样式
style = xlwt.XFStyle()# 2. 为样式创建背景图案(pattern)
pattern = xlwt.Pattern()# 3. 指定背景颜色
pattern.pattern = xlwt.Pattern.SOLID_PATTERN  # 设置背景颜色模式
pattern.pattern_fore_colour = 26  # 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...
# [...其他属性掠过...]# 4. 设定背景图案样式
style.pattern = pattern# 5. 带样式将内容写入Excel
sheet.write(1,2,'EnglishTest',style)  # style代表上面定义的样式
# ...代码掠过...

2)代码示例说明指定背景各属性

# 导入xlwt模块
import xlwt# 创建一个workbook对象,就相当于创建了一个Excel文件
book = xlwt.Workbook(encoding='utf-8',style_compression=0)  # encoding:设置编码,可写中文;style_compression:是否压缩,不常用
# 创建一个sheet对象,相当于创建一个sheet页
sheet = book.add_sheet('test_sheet',cell_overwrite_ok=True) # cell_overwrite_ok:是否可以覆盖单元格,默认为False# 初始化样式
style = xlwt.XFStyle()# 1. 为样式创建字体(font)
font = xlwt.Font()
# 指定字体的具体属性(仅列出常用属性)
font.name = 'Times New Roman'   # 指定字体
font.height = 300   # 和excel字体大小比例是1:20
font.bold = True    # 字体是否加粗
font.underline = True   # 字体是否下划线
font.struck_out = True  # 字体是否有横线
font.italic = True  # 是否斜体字
font.colour_index = 4   # 字体颜色# 2. 为样式创建背景图案(pattern)
pattern = xlwt.Pattern()
# 指定背景颜色
pattern.pattern = xlwt.Pattern.SOLID_PATTERN  # 设置背景颜色模式
pattern.pattern_fore_colour = 3    # 不同的值代表不同颜色背景# 设置style的各个属性的样式
style.font = font   # 设定字体样式
style.pattern = pattern # 设定背景图案样式# 向sheet页中添加数据
text = '字串测试'
sheet.write(1,2,'EnglishTest',style)  # 第1行第2列,写入'EnglishTest';从第0行开始计数
sheet.write(2,2,'中文测试',style)
sheet.write(3,2,text,style)# 将以上内容保存到指定的文件中
book.save('blog.xls')   # 在字符串前加r,声明为raw字符串,这样就不会处理其中的转义了

3)执行结果

4)补充字体颜色对照图

见字体的颜色对比表;和字体颜色映射相同

2.2.3 边框设置(borders)

1)语法描述

# ...代码掠过...# 1. 初始化样式
style = xlwt.XFStyle()# 2. 为样式创建边框(borders)
# borders.left = xlwt.Borders.THIN
#     NO_LINE: 官方代码中NO_LINE所表示的值为0,没有边框
#     THIN:官方代码中THIN所表示的值为1,边框为实线
borders = xlwt.Borders()# 3. 设定边框属性
borders.left = xlwt.Borders.THIN
borders.right = xlwt.Borders.THIN
borders.top = xlwt.Borders.THIN
borders.bottom = xlwt.Borders.THIN
# [...其他属性掠过...]# 4. 设置边框样式
style.borders = borders# 5. 带样式将内容写入Excel
sheet.write(1,2,'EnglishTest',style)  # style代表上面定义的样式# ...代码掠过...

2)代码示例说明指定边框各属性

# 导入xlwt模块
import xlwt# 创建一个workbook对象,就相当于创建了一个Excel文件
book = xlwt.Workbook(encoding='utf-8',style_compression=0)  # encoding:设置编码,可写中文;style_compression:是否压缩,不常用
# 创建一个sheet对象,相当于创建一个sheet页
sheet = book.add_sheet('test_sheet',cell_overwrite_ok=True) # cell_overwrite_ok:是否可以覆盖单元格,默认为False# 初始化样式
style = xlwt.XFStyle()# 1. 为样式创建字体(font)
font = xlwt.Font()
# 指定字体的具体属性(仅列出常用属性)
font.name = 'Times New Roman'   # 指定字体
font.height = 300   # 和excel字体大小比例是1:20
font.bold = True    # 字体是否加粗
font.underline = True   # 字体是否下划线
font.struck_out = True  # 字体是否有横线
font.italic = True  # 是否斜体字
font.colour_index = 4   # 字体颜色# 2. 为样式创建背景图案(pattern)
pattern = xlwt.Pattern()
# 指定背景颜色
pattern.pattern = xlwt.Pattern.SOLID_PATTERN  # 设置背景颜色模式
pattern.pattern_fore_colour = 3    # 不同的值代表不同颜色背景# 3. 为样式创建边框(borders)
borders = xlwt.Borders()
# 设定边框属性
borders.left = xlwt.Borders.THIN
borders.right = xlwt.Borders.THIN
borders.top = xlwt.Borders.THIN
borders.bottom = xlwt.Borders.THIN# 设置style的各个属性的样式
style.font = font   # 设定字体样式
style.pattern = pattern # 设定背景图案样式
style.borders = borders     # 设定边框样式# 向sheet页中添加数据
text = '字串测试'
sheet.write(1,2,'EnglishTest',style)  # 第1行第2列,写入'EnglishTest';从第0行开始计数
sheet.write(2,2,'中文测试',style)
sheet.write(3,2,text,style)# 将以上内容保存到指定的文件中
book.save('blog.xls')   # 在字符串前加r,声明为raw字符串,这样就不会处理其中的转义了

3)执行结果

2.2.4 对齐方式设置(alignment)

1)语法描述

# ...代码掠过...# 1. 初始化样式
style = xlwt.XFStyle()# 2. 对齐方式的设置(alignment)
alignment = xlwt.Alignment()# 3. 设置具体的对齐方式# vert代表垂直对齐方式;horz代表水平对齐方式
alignment.vert = 0x01   # 0x00 上端对齐;0x01 居中对齐(垂直方向上);0x02 底端对齐
alignment.horz = 0x03   # 0x01 左端对齐;0x02 居中对齐(水平方向上);0x03 右端对齐
# [...其他属性掠过...]# 4. 设定对齐方式
style.alignment = alignment# 5. 带样式将内容写入Excel
sheet.write(1,2,'EnglishTest',style)  # style代表上面定义的样式# ...代码掠过...

2)代码示例说明指定字体各属性

# 导入xlwt模块
import xlwt# 创建一个workbook对象,就相当于创建了一个Excel文件
book = xlwt.Workbook(encoding='utf-8',style_compression=0)  # encoding:设置编码,可写中文;style_compression:是否压缩,不常用
# 创建一个sheet对象,相当于创建一个sheet页
sheet = book.add_sheet('test_sheet',cell_overwrite_ok=True) # cell_overwrite_ok:是否可以覆盖单元格,默认为False# 初始化样式
style = xlwt.XFStyle()# 1. 为样式创建字体(font)
font = xlwt.Font()
# 指定字体的具体属性(仅列出常用属性)
font.name = 'Times New Roman'   # 指定字体
font.height = 300   # 和excel字体大小比例是1:20
font.bold = True    # 字体是否加粗
font.underline = True   # 字体是否下划线
font.struck_out = True  # 字体是否有横线
font.italic = True  # 是否斜体字
font.colour_index = 4   # 字体颜色# 2. 为样式创建背景图案(pattern)
pattern = xlwt.Pattern()
# 指定背景颜色
pattern.pattern = xlwt.Pattern.SOLID_PATTERN  # 设置背景颜色模式
pattern.pattern_fore_colour = 3    # 不同的值代表不同颜色背景# 3. 为样式创建边框(borders)
borders = xlwt.Borders()
# 设定边框属性
borders.left = xlwt.Borders.THIN
borders.right = xlwt.Borders.THIN
borders.top = xlwt.Borders.THIN
borders.bottom = xlwt.Borders.THIN# 4. 对齐方式的设置(alignment)
alignment = xlwt.Alignment()
# 设置具体的对齐方式# vert代表垂直对齐方式;horz代表水平对齐方式
alignment.vert = 0x01   # 0x00 上端对齐;0x01 居中对齐(垂直方向上);0x02 底端对齐
alignment.horz = 0x03   # 0x01 左端对齐;0x02 居中对齐(水平方向上);0x03 右端对齐# 设置style的各个属性的样式
style.font = font   # 设定字体样式
style.pattern = pattern # 设定背景图案样式
style.borders = borders     # 设定边框样式
style.alignment = alignment     # 设定对齐方式# 向sheet页中添加数据
text = '字串测试'
sheet.write(1,2,'EnglishTest',style)  # 第1行第2列,写入'EnglishTest';从第0行开始计数
sheet.write(2,2,'中文测试',style)
sheet.write(3,2,text,style)# 将以上内容保存到指定的文件中
book.save('blog.xls')   # 在字符串前加r,声明为raw字符串,这样就不会处理其中的转义了

3)执行结果

3. 脚本实战

1)题目描述

以下是ansible.conf配置文件内内容,读取ansible.conf,并指定格式写入到excel(abcd.xls)文件的hostInfo sheet页中
格式要求:宋体,20,黄色背景,有边框,水平居中

写入后的格式展示:

2)代码实现

import xlwtdef set_style(fontName,fontSize,backColour,isBorder,ahorz):style = xlwt.XFStyle()# 1. 字体属性font = xlwt.Font()font.name = fontNamefont.height = fontSize# 2. 背景属性pattern = xlwt.Pattern()pattern.pattern = xlwt.Pattern.SOLID_PATTERNpattern.pattern_fore_colour = backColour# 3. 是否边框borders = xlwt.Borders()if isBorder == True:borders.left = xlwt.Borders.THINborders.right = xlwt.Borders.THINborders.top = xlwt.Borders.THINborders.bottom = xlwt.Borders.THIN# 4. 对齐方式alignment = xlwt.Alignment()if ahorz == 'left':alignment.horz = 0x01elif ahorz == 'center':alignment.horz = 0x02elif ahorz == 'right':alignment.horz == 0x03style.font = font  # 设定字体样式style.pattern = pattern  # 设定背景图案样式style.borders = borders  # 设定边框样式style.alignment = alignment  # 设定对齐方式return styledef writeFunc(srcFileName,dstFileName,sheetName):book = xlwt.Workbook(encoding='utf-8',style_compression=0)sheet = book.add_sheet(sheetName,cell_overwrite_ok=True)with open(srcFileName,'r',encoding='utf-8') as f:rowNum = 0for line in f:  # 遍历文件一行数据line_new1 = line.strip().split(' ')     # 列表形式返回每行数据colNum = 0for i in line_new1:     # 遍历每行数据组成的列表content = i.strip().split('=')[1]   # 返回列表中每个元素if '"' in content:content = content.replace("\"","")      # 替换双引号 "sheet.write(rowNum,colNum,content,set_style('宋体',400,5,True,'center'))      # 向单元格写入数据colNum += 1rowNum += 1book.save(dstFileName)if __name__ == '__main__':writeFunc('ansible.conf','abcd.xls','hostInfo')

python模块 之 xlwt模块相关推荐

  1. python中 xlrd/xlwt模块详解

    python中 xlrd/xlwt模块详解 1.什么是xlrd模块 python操作excel主要用到xlrd和xlwt两个库,即xlrd是读excel,xlwt是写excel库 一.安装xlrd模块 ...

  2. python没有这个xlwt模块_python xlwt模块简介

    importxlwtclassWorkbook(object0):'''工作簿类,使用xlwt创建excel文件时,首先要实例化此类的对象''' def __init__(self, encoding ...

  3. python读写xlsx文件_python读写Excel文件--使用xlrd模块读取,xlwt模块写入

    一.安装xlrd模块和xlwt模块 1. 下载xlrd模块和xlwt模块 到python官网http://pypi.python.org/pypi/xlrd下载模块.下载的文件例如:xlrd-0.9. ...

  4. python xlwt写入excel_python xlwt模块生成excel文件并写入数据 xlrd读取数据

    python中一般使用 xlwt (excel write)来生成Excel文件(可以控制单元格格式),用 xlrd 来读取Excel文件,用xlrd读取excel是不能对其进行操作的. 1.xlrd ...

  5. Python练习篇25-re模块利用正则匹配提取网页邮箱并保存

    本文介绍re模块利用正则匹配提取网页邮箱并保存 提取网页邮箱我们需要用到requests模块.re模块和xlwt模块(下载方法:打开cmd,输入pip install 包名),负责去匹配邮箱数据. 本 ...

  6. python如何使用ppip安装xlwt_Python中xlrd和xlwt模块使用方法

    原博文 2017-07-05 21:30 − 本文主要介绍可操作excel文件的xlrd.xlwt模块.其中xlrd模块实现对excel文件内容读取,xlwt模块实现对excel文件的写入. 安装xl ...

  7. Python Excel 操作 | xlrd+xlwt 模块笔记

    Python 的pandas模块使用xlrd作为读取 excel 文件的默认引擎.但是,xlrd在其最新版本(从 2.0.1 版本开始)中删除了对 xls 文件以外的任何文件的支持. xlsx fil ...

  8. python怎么用excel-Python使用xlwt模块操作Excel的方法详解

    本文实例讲述了Python使用xlwt模块操作Excel的方法.分享给大家供大家参考,具体如下: 部分摘自官网文档. 该模块安装很简单 $ pip install xlwt 先来个简单的例子: #!/ ...

  9. python xlwt xlrd模块详解_python操作excel之xlrd、xlwt模块详解

    python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库. 可从这里下载https://pypi.python.org/pypi.下面分别记录 ...

最新文章

  1. linux权限切换命令,Linux基础常用命令汇总(权限操作)
  2. python中定义的函数不掉用不会执行_如果出现异常/错误,如何不在python中停止执行其他函数...
  3. java 原理图_Java中比较重要的原理图(三大框架、、、、)
  4. Spring入门(四)之BeanFactory
  5. php任务奖励体系,phpwind7.5完备的积分体系
  6. 【答辩问题】计算机专业本科毕业设计答辩自述2
  7. 技巧:如何从苹果Mac跟踪设备上所有电池的电量?
  8. ExtJs教程 3.0
  9. 虚拟服务器建网站苹果cms,零基础搭建苹果cmsv10影视站教程
  10. 计算机如何安装无线网络适配器,无线网卡驱动怎么安装?电脑无线网卡驱动2种安装方法...
  11. 模拟将本地文件上传至外服务器
  12. 蹩脚英语——Translation Of Model Test Two
  13. open judge 1.7.1
  14. 程序员如何进行职业规划?
  15. 迷宫寻径--试探回溯法
  16. 《深入理解计算机系统》学习记录
  17. CSDN开发者周刊第 21期:Wi-Fi 之父辞世,谷歌服务器再次全球宕机;Windows 10 将支持安卓应用
  18. 利用python进行数据分析数据集_《利用Python进行数据分析》终章·数据分析案例·学习笔记(二)...
  19. 配置vscode终端字体
  20. 创新案例|语言教育App头牌Duolingo如何重新点燃用户增长350%

热门文章

  1. 使用Linkage Mapper制作环境连接图
  2. 操作系统---linux
  3. XB文件开发详解(上报证监会文件)_入门系列
  4. 亚马逊、沃尔玛自养号大额下单需要解决哪些问题?
  5. Error handling response: TypeError: Cannot read property ‘1‘ of null
  6. [curl] 开启代理加速下载
  7. 数字技术战略:开发者体验 —— 内部工具的“最后一公里”
  8. 电影解说类自媒体如何才能脱颖而出
  9. Linux无线网卡驱动更新
  10. OpenJudge百炼-2745-显示器-C语言-模拟