xlwt 模块操作 excel表格, 填充颜色

news/2024/11/23 5:35:10/
# 1、xlwt 模块的初级使用
import xlwt
# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding='utf-8')
# 创建一个worksheet# 创建一个workbook 设置编码
worksheet = workbook.add_sheet('My Worksheet')  # 表名
# 写入excel
# 参数对应 行, 列, 值
worksheet.write(2, 1, label='this is test')
# 保存
workbook.save('./excel/1Excel_test1.xls')# 2、加上自定义样式
import xlwt
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('My Worksheet2')
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('./excel/2formatting2.xls')  # 保存文件# 3、 设置单元格宽度
import xlwt
# workbook = xlwt.Workbook()  # 默认ascii码
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('My Sheet3')
worksheet.write(0, 0, 'My Cell Contents')
worksheet.write(2, 2, 'My Cell Contents2')
# 设置单元格宽度
worksheet.col(0).width = 10000
# worksheet.col(2).width = 4440
workbook.save('./excel/3cell_width3.xls')# 4、输入一个日期到单元格
import xlwt
import datetime
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('My Sheet4')
style = xlwt.XFStyle()
style.num_format_str = 'M/D/YY'
# Other options: D-MMM-YY, D-MMM, MMM-YY, h:mm, h:mm:ss, h:mm, h:mm:ss, M/D/YY h:mm, mm:ss, [h]:mm:ss, mm:ss.0
worksheet.write(0, 0, datetime.datetime.now(), style)  # 带样式的写入
workbook.save('./excel/4Excel_Workbook4.xls')# 5、向单元格添加一个公式
import xlwt
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('My Sheet5')
worksheet.write(0, 0, 5)  # Outputs 5
worksheet.write(0, 1, 2)  # Outputs 2
worksheet.write(1, 0, xlwt.Formula('A1*B1'))  # Should output "10" (A1[5] * A2[2])
worksheet.write(1, 1, xlwt.Formula('SUM(A1,B1)'))  # Should output "7" (A1[5] + A2[2])
workbook.save('./excel/5Excel_Workbook.xls')# 6、向单元格添加一个超链接
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet6')
worksheet.write(0, 0, xlwt.Formula('HYPERLINK("http://www.google.com";"Google")'))
# Outputs the text "Google" linking to http://www.google.com
workbook.save('./excel/6Excel_Workbook6.xls')# 7、 合并列和行
import xlwt
workbook = xlwt.Workbook(encoding="utf-8")
worksheet = workbook.add_sheet('My Sheet7')# 前两个数字代表行,后两个数字表示列
worksheet.write_merge(0, 0, 0, 3, 'First Merge')  # Merges row 0's columns 0 through 3.
font = xlwt.Font()  # Create Font
font.bold = True  # Set font to Bold
style = xlwt.XFStyle()  # Create Style   给文本加格式
style.font = font  # Add Bold Font to Style
worksheet.write_merge(1, 2, 0, 3, 'Second Merge', style)  # Merges row 1 through 2's columns 0 through 3.
workbook.save('./excel/7Excel_Workbook.xls')# 8、 设置单元格内容的对其方式
import xlwt
workbook = xlwt.Workbook(encoding="utf-8")
worksheet = workbook.add_sheet('My Sheet8')
alignment = xlwt.Alignment()  # Create Alignment
alignment.horz = xlwt.Alignment.HORZ_CENTER
#May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
alignment.vert = xlwt.Alignment.VERT_CENTER
#May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
style = xlwt.XFStyle()  # Create Style
style.alignment = alignment  # Add Alignment to Style
worksheet.write(0, 0, 'Cell Contents', style)
workbook.save('./excel/8Excel_Workbook.xls')# 9、为单元格议添加边框
import xlwt
workbook = xlwt.Workbook(encoding="utf-8")
worksheet = workbook.add_sheet('My Sheet9')
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.NO_LINE
borders.bottom = xlwt.Borders.THIN
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/9Excel_Workbook.xls')# 10、为单元格设置背景色
import xlwt
workbook = xlwt.Workbook(encoding="utf-8")
worksheet = workbook.add_sheet('My Sheet10')
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/10Excel_Workbook.xls')

 


http://www.ppmy.cn/news/799417.html

相关文章

Excel中根据输入内容 自动填充时间、自动增加边框、自动添加背景颜色

目录 所用相关Excel中函数了解 1、以下拉列表的方式在“商品名称”一列,根据需要选择对应商品的名称 1.1 快速制作下拉列表 1.2 选择“时间”下面的第一个单元格,输入一下组合函数: 1.3 这个问题是关键,如何解决呢&#x…

JAVA 浏览器下载excel,自定义样式:合并单元格,设置多种背景填充颜色,冻结窗格

添加依赖&#xff1a; <!-- POI --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId&…

excel根据单元格内容设定行列颜色

1. 根据单元格内容设定单元格颜色 【开始】-【条件格式】-【突出显示单元格规则】-【选择规则】-【设置样式】 2. 根据单元格内容设定所在行/列内容 【开始】-【条件格式】-【新建规则】-【是用公式确定...】-【值】-【样式】 注意选择单元格&#xff0c;英文符号

EasyExcel 使用和背景颜色样式(3.0以上的版本)

谈到新技术&#xff0c;每个人都会有点恐惧&#xff0c;怕处理不好&#xff0c;确实第一次使用新技术会遇到很多坑&#xff0c;本人以前使用poi也遇到了很多的坑&#xff0c;但是很快就解决了&#xff0c;这次使用EasyExcel这个新技术去做excel表的导出&#xff0c;还要给表格加…

EasyExcel使用: RGB字体,RGB背景颜色,fillForegroundColor颜色对照表

使用EasyExcel导出表格可能会对字体颜色和单元格背景颜色进行自定义的修改。 可以自定义字体颜色或者每个单元格的颜色 要想自定义颜色&#xff0c;需要重写CellWriteHandler接口&#xff0c;实现 public void afterCellDispose(CellWriteHandlerContext context);&#xff0…

Excel如何隔一行或几行填充颜色

如下图是一份Excel表格&#xff0c;现在我们想要将表格隔一行填充蓝色。 全选要填充颜色的表格单元格区域 点击【条件格式】&#xff0c;选择【新建规则】 选择【使用公式确定要设置格式的单元格】&#xff0c;然后输入公式MOD(ROW(),2)1&#xff0c;接着点击格式设置要填充的颜…

Excel多列数据值比较大小,改变填充颜色

Example&#xff1a;将D和E分别与C比较&#xff0c;将D与E中较大的值填充颜色。 第一步&#xff1a;选择需要与目标列作比较的范围&#xff1a;即本例中为D2:E29 注意&#xff1a;看清此时起始单元格&#xff0c;本例中为C2。如果不清楚的请看表格左上角。 第二步&#xff1a;…

easyExcel设置单个单元格(颜色)样式

背景&#xff1a;需求是使用excel设置目标单元格的样式&#xff08;颜色&#xff09;&#xff0c;但我之前没有学过easyExcel&#xff0c;在网上找资料的时候&#xff0c;发现有关easyExcel相关的单个单元格样式设置的资料比较少&#xff0c;有的还源码不全&#xff0c;只能说用…