Python | Python uses the xlwt module to operate Excel (continuously updated)

One, install xlwt

pip install xlwt

Two, write to excel

1. Code

import xlwt

workbook = xlwt.Workbook(encoding='utf8')
worksheet = workbook.add_sheet('sheet1')
# 初始化样式
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, '这是一个测试')
# 带样式的写入
worksheet.write(1, 0, '这是一个测试', style)
# 保存文件
workbook.save('C:\\Users\\Jonsson\\Desktop\\234.xls')

2. Effect

Insert picture description here

Three, set the cell height and width

1. Code

# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding='utf-8')
# 创建一个worksheet
worksheet = workbook.add_sheet('LOL')
# 设置单元格宽度大小
worksheet.col(i).width = 4000
# 设置单元格高度大小
style = xlwt.easyxf('font:height 300')
worksheet.row(i).set_style(style)
workbook.save('C:\\Users\\Jonsson\\Desktop\\lol.xls')

2. Effect

Insert picture description here

Guess you like

Origin blog.csdn.net/y1534414425/article/details/106692226