本文需要以下的库文件,读者可以按照下方的方法进行安装:
"""
需安装的库文件:
pip install xlwt
pip install patterns
换源网址:
- 豆瓣:http://pypi.douban.com/simple/
- 中科大:https://pypi.mirrors.ustc.edu.cn/simple/
- 清华:https://pypi.tuna.tsinghua.edu.cn/simple
换源安装,例如:pip install xlwt -i https://pypi.tuna.tsinghua.edu.cn/simple
"""
首先使用xlwt简单写入一个excel文件:
import xlwt
workbook = xlwt.Workbook(encoding='utf-8')
sheet = workbook.add_sheet("对应的子表格", cell_overwrite_ok=True)
row = 0
col = 0
data = "写入的数据"
sheet.write(row, col, data)
workbook.save("编写文件.xls")
效果如下:
使用xlwt将一行数据写入excel文件中:
import xlwt
import patterns as patterns
workbook = xlwt.Workbook(encoding='utf-8')
sheet = workbook.add_sheet("对应的子表格", cell_overwrite_ok=True)
row = 0
col = 0
data = ["班级", "姓名", "性别", "年龄"]
num_row = 0
num_col = 0
for i in data:
sheet.write(row, num_col, i)
num_col += 1
workbook.save("编写文件.xls")
效果如下:
使用xlwt将多行数据写入excel文件中:
workbook = xlwt.Workbook(encoding='utf-8')
sheet = workbook.add_sheet("对应的子表格", cell_overwrite_ok=True)
row = 0
col = 0
data_list = [["班级", "姓名", "性别", "年龄"], ["一年级", "张三", "男", "7"], ["二年级", "李四", "男", "9"], ["五年级", "王五", "女", "12"]]
num_row = 0
num_col = 0
for data in data_list:
for i in data:
sheet.write(num_row, num_col, i)
num_col += 1
num_row += 1
num_col = 0
workbook.save("编写文件.xls")
效果如下: 讲完xlwt写入数据的方法,下面讲述设计excel表格格式的方法:
1.字体设计:
style = xlwt.XFStyle()
font = xlwt.Font()
font.name = u'宋体'
font.height = 11*20
font.colour_index = 1
font.bold = True
font.underline = True
font.underline = True
举个例子:
import xlwt
import patterns as patterns
workbook = xlwt.Workbook(encoding='utf-8')
sheet = workbook.add_sheet("对应的子表格", cell_overwrite_ok=True)
style = xlwt.XFStyle()
font = xlwt.Font()
font.name = u'宋体'
font.height = 11*20
font.colour_index = 0
font.bold = True
font.underline = True
font.underline = True
row = 0
col = 0
data_list = [["班级", "姓名", "性别", "年龄"], ["一年级", "张三", "男", "7"], ["二年级", "李四", "男", "9"], ["五年级", "王五", "女", "12"]]
num_row = 0
num_col = 0
style.font = font
for data in data_list:
for i in data:
sheet.write(num_row, num_col, i, style)
num_col += 1
num_row += 1
num_col = 0
workbook.save("编写文件.xls")
效果图如下:
2.单元格设计
(1)单元格背景颜色
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = 1
举个例子:
import xlwt
import patterns as patterns
workbook = xlwt.Workbook(encoding='utf-8')
sheet = workbook.add_sheet("对应的子表格", cell_overwrite_ok=True)
style = xlwt.XFStyle()
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = 3
row = 0
col = 0
data_list = [["班级", "姓名", "性别", "年龄"], ["一年级", "张三", "男", "7"], ["二年级", "李四", "男", "9"], ["五年级", "王五", "女", "12"]]
num_row = 0
num_col = 0
style.pattern = pattern
for data in data_list:
for i in data:
sheet.write(num_row, num_col, i, style)
num_col += 1
num_row += 1
num_col = 0
workbook.save("编写文件.xls")
效果图如下:
(2)单元格对齐方式
alignment = xlwt.Alignment()
alignment.horz = 0x02
alignment.vert = 0x01
alignment.wrap = 1
举个例子:
import xlwt
import patterns as patterns
workbook = xlwt.Workbook(encoding='utf-8')
sheet = workbook.add_sheet("对应的子表格", cell_overwrite_ok=True)
style = xlwt.XFStyle()
alignment = xlwt.Alignment()
alignment.horz = 0x02
alignment.vert = 0x01
alignment.wrap = 1
row = 0
col = 0
data_list = [["班级", "姓名", "性别", "年龄"], ["一年级", "张三", "男", "7"], ["二年级", "李四", "男", "9"], ["五年级", "王五", "女", "12"]]
num_row = 0
num_col = 0
style.alignment = alignment
for data in data_list:
for i in data:
sheet.write(num_row, num_col, i, style)
num_col += 1
num_row += 1
num_col = 0
workbook.save("编写文件.xls")
效果图如下:
(3) 单元格边框设置
borders = xlwt.Borders()
borders.left = 1
borders.right = 1
borders.top = 1
borders.bottom = 1
borders.left_colour = 1
borders.right_colour = 1
borders.top_colour = 1
borders.bottom_colour = 1
举个例子:
import xlwt
import patterns as patterns
workbook = xlwt.Workbook(encoding='utf-8')
sheet = workbook.add_sheet("对应的子表格", cell_overwrite_ok=True)
style = xlwt.XFStyle()
borders = xlwt.Borders()
borders.left = 2
borders.right = 2
borders.top = 2
borders.bottom = 2
borders.left_colour = 0
borders.right_colour = 0
borders.top_colour = 0
borders.bottom_colour = 0
row = 0
col = 0
data_list = [["班级", "姓名", "性别", "年龄"], ["一年级", "张三", "男", "7"], ["二年级", "李四", "男", "9"], ["五年级", "王五", "女", "12"]]
num_row = 0
num_col = 0
style.borders = borders
for data in data_list:
for i in data:
sheet.write(num_row, num_col, i, style)
num_col += 1
num_row += 1
num_col = 0
workbook.save("编写文件.xls")
效果图如下:
(4)单元格行高和列宽
sheet.col(1).width = 15 * 256
tall_style = xlwt.easyxf('font:height 720;')
first_row = sheet.row(0)
first_row.set_style(tall_style)
举个例子:
import xlwt
import patterns as patterns
workbook = xlwt.Workbook(encoding='utf-8')
sheet = workbook.add_sheet("对应的子表格", cell_overwrite_ok=True)
style = xlwt.XFStyle()
row = 0
col = 0
data_list = [["班级", "姓名", "性别", "年龄"], ["一年级", "张三", "男", "7"], ["二年级", "李四", "男", "9"], ["五年级", "王五", "女", "12"]]
num_row = 0
num_col = 0
for data in data_list:
for i in data:
sheet.col(num_col).width = 15 * 256
tall_style = xlwt.easyxf('font:height 720;')
first_row = sheet.row(num_row)
first_row.set_style(tall_style)
sheet.write(num_row, num_col, i)
num_col += 1
num_row += 1
num_col = 0
workbook.save("编写文件.xls")
效果图如下:
(5)合并单元格
sheet.write_merge(first_row, last_row, first_col, last_col, u'合并')
举个例子:
import xlwt
import patterns as patterns
workbook = xlwt.Workbook(encoding='utf-8')
sheet = workbook.add_sheet("对应的子表格", cell_overwrite_ok=True)
style = xlwt.XFStyle()
row = 0
col = 0
data_list = [["班级", "姓名", "性别", "年龄"], ["一年级", "张三", "男", "7"], ["二年级", "李四", "男", "9"], ["五年级", "王五", "女", "12"]]
num_row = 0
num_col = 0
for data in data_list:
for i in data:
sheet.write(num_row, num_col, i)
num_col += 1
num_row += 1
num_col = 0
sheet.write_merge(1, 3, 1, 3, u'合并')
workbook.save("编写文件.xls")
效果图如下: 完整代码如下:
import xlwt
import patterns as patterns
workbook = xlwt.Workbook(encoding='utf-8')
sheet = workbook.add_sheet("对应的子表格", cell_overwrite_ok=True)
style = xlwt.XFStyle()
"""
字体设计
"""
font = xlwt.Font()
font.name = u'宋体'
font.height = 11*20
font.colour_index = 0
font.bold = True
font.underline = True
font.underline = True
"""
单元格背景颜色
"""
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = 1
"""
单元格对齐方式
"""
alignment = xlwt.Alignment()
alignment.horz = 0x02
alignment.vert = 0x01
alignment.wrap = 1
"""
单元格边框设置
"""
borders = xlwt.Borders()
borders.left = 1
borders.right = 1
borders.top = 1
borders.bottom = 1
borders.left_colour = 0
borders.right_colour = 0
borders.top_colour = 0
borders.bottom_colour = 0
row = 0
col = 0
data_list = [["班级", "姓名", "性别", "年龄"], ["一年级", "张三", "男", "7"], ["二年级", "李四", "男", "9"], ["五年级", "王五", "女", "12"]]
num_row = 0
num_col = 0
style.font = font
style.pattern = pattern
style.alignment = alignment
style.borders = borders
for data in data_list:
for i in data:
"""
单元格行高和列宽
"""
sheet.col(num_col).width = 15 * 256
tall_style = xlwt.easyxf('font:height 720;')
first_row = sheet.row(num_row)
first_row.set_style(tall_style)
sheet.write(num_row, num_col, i, style)
num_col += 1
num_row += 1
num_col = 0
workbook.save("编写文件.xls")
效果图如下:
下面是颜色图,上述代码涉及颜色的代码均在图上: 在此感谢博主@想学机器学习的rookie的博文:Python xlwt模块设置Excel样式 该博主的文章写得很详细,我这边做些补充,读者可以去看看博主@想学机器学习的rookie的文章,本次的博文写到这里了,欢迎大家的点赞,评论和收藏一波,代码中有错误或纰漏处也欢迎各位指出,我会在第一时间进行修改的。
|