xlwt的学习记录和相关问题

xlwt的基础认知

xlwt是python中Excel文件写入的扩展工具,xlrd则用于Excel文件读取

1、安装

pip install xlwt
pip install xlwt

2、指令

import xlwt
import datetime  # 导入日期库
# 创建一个workbook对象,相当于创建一个Excel文件
book = xlwt.Workbook(encoding='utf-8', style_compression=0)

# 创建一个sheet对象,相当于创建一个Excel文件中的一个表格
sheet = book.add_sheet('test', cell_overwrite_ok=True)

sheet2 = book.add_sheet('sheet2', cell_overwrite_ok=True)

# test为表名称,cell_overwrite_ok表示是否覆盖单元格,其实是worksheet的实例化一个

# 为内容设置style
font = xlwt.Font()
# 字体
font.name = 'Time New Roman'
# 加粗
font.bold = True
# 下划线
font.underline = True
# 斜线
font.italic = True
# 设置字体颜色
font.colour_index = xlwt.Style.colour_map['black']

# 设置单元格
# 初始化
alignment = xlwt.Alignment()
# 水平居中
alignment.horz = xlwt.Alignment.HORZ_CENTER
# 垂直居中
alignment.vert = xlwt.Alignment.VERT_CENTER

# 设置单元格边框
borders = xlwt.Borders()
# DASHED虚线
# NO_LINE没有
# THIN实线
borders.left = xlwt.Borders.DASHED
# 设置颜色
borders.left_colour = 0x40b

borders.right = xlwt.Borders.DASHED
borders.right_colour = 0x40b

borders.top = xlwt.Borders.DASHED
borders.top_colour = 0x40b

borders.bottom = xlwt.Borders.DASHED
borders.bottom_colour = 0x40b

# 设置单元格背景色
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
# 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...
pattern.pattern_fore_colour = 2

# 初始化并创建style
style = xlwt.XFStyle()
style.font = font
style.alignment = alignment
style.borders = borders
style.pattern = pattern

# 为test增添数据,参数对于 行、列、值
sheet.write(0, 0, 'www.baidu.com')
# 添加style
sheet.write(1, 0, 'baidu', style)

txt1 = '向日葵'
sheet.write(0, 1, txt1)

txt2 = 'python实战'
sheet.write(1, 1, txt2.encode('utf-8').decode('utf-8'))  # 将中文解码成Unicode字符 否则报错

# 设置单元格宽度
sheet.col(0).width = 5555
# 设置单元格高度
sheet.row(1).height_mismatch = True
sheet.row(1).height = 5555

# 有样式加入
sheet.write(2, 1, 'flower0', style)

# 在单元格加入日期
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
sheet.write(2, 0, datetime.datetime.now(), style)

# 合并单元格,调用merge()函数
sheet.write_merge(4, 4, 0, 1, 'first merge')

sheet.write_merge(5, 6, 0, 0, 'second merge', style)

# 在单元格中添加公式
sheet.write(8, 0, 5)
sheet.write(8, 1, 6)
sheet.write(8, 2, xlwt.Formula('A9*B9'))
sheet.write(8, 3, xlwt.Formula('SUM(A9,B9)'))

# 添加超链接
sheet.write(9, 0, xlwt.Formula('HYPERLINK("https://www.baidu.com";"baidu")'))

# 在字符串前加r,申明为row字符串,这样就不会处理里面的转义了,
book.save(r'./xlwt/test1.xls')

xlwt学习错误总结

1、错误一:没事找事
AttributeError: ‘str’ object has no attribute ‘decode’
解决方案:
sheet.write(0, 1, txt1.decode(‘utf-8’)) # 错误
改为
Sheet.write(0, 1, tst1.encode(‘utf-8’).decode(‘utf-8’) # 正确

原因:py3本来就是unicode了,不需要再解码

2、
错误二:想太多
xlwt的学习记录和相关问题_第1张图片assert 0 <= c1 <= c2 <= 255
解决方法:
xlwt的学习记录和相关问题_第2张图片
原因:注意:write_merge的函数定义:
def write_merge(self, r1, r2, c1, c2, label="", style=Style.default_style):
其中:0 <= c1 <= c2 <= 255
0 <= r1 <= r2 <= 65535
参考链接 [1]: https://jingyan.baidu.com/article/c1465413edc97c0bfdfc4c5c.html

你可能感兴趣的:(xlwt的学习记录和相关问题)