python 包之 xlwt 操作 excel 教程
一、安装
pip install xlwt
二、创建表格并写入
-
创建表格,设置sheet名称
-
写入指定行列的数据,将表格进行保存
import xlwt
# 创建一个workbook并设置编码
workbook = xlwt.Workbook(encoding = 'utf-8')
# 添加sheet
worksheet = workbook.add_sheet('飞兔小哥')
# 写入excel, 参数对应 行, 列, 值
worksheet.write(1,0, label = '我爱autofelix')
# 保存
workbook.save('xlwt教学.xls')
三、设置单元格样式
-
给单元格设置样式,包括字体颜色、粗细、是否斜体等
import xlwt
workbook = xlwt.Workbook(encoding = 'ascii')
worksheet = workbook.add_sheet('飞兔小哥')
# 初始化样式
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, '带样式的单元格', style)
# 保存文件
workbook.save('xwlt单元格样式教学.xls')
四、设置单元格宽度
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('飞兔小哥')
worksheet.write(0, 0,'我爱autofelix')
# 设置单元格宽度
worksheet.col(0).width = 3000
workbook.save('xwlt单元格宽度教学.xls')
五、设置单元格背景色
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('飞兔小哥')
# 创建模式对象Create the Pattern
pattern = xlwt.Pattern()
# May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
#设置模式颜色 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...
pattern.pattern_fore_colour = 5
# 创建样式对象Create the Pattern
style = xlwt.XFStyle()
# 将模式加入到样式对象Add Pattern to Style
style.pattern = pattern
#向单元格写入内容时使用样式对象style
worksheet.write(0, 0, '单元格内容', style)
workbook.save('xwlt单元格背景色教学.xls')
六、设置单元格内容对其方式
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('飞兔小哥')
# 创建对其格式的对象 Create Alignment
alignment = xlwt.Alignment()
#水平居中 May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
alignment.horz = xlwt.Alignment.HORZ_CENTER
#我上下对齐 May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
alignment.vert = xlwt.Alignment.VERT_CENTER
#创建样式对象 Create Style
style = xlwt.XFStyle()
# 将格式Alignment对象加入到样式对象Add Alignment to Style
style.alignment = alignment
#写入的时候调用样式style
worksheet.write(0, 0, '单元居中', style)
workbook.save('xlwt单元格对齐教学.xls')
七、单元格添加超链接
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('飞兔小哥')
worksheet.write(0, 0, xlwt.Formula('HYPERLINK("https://www.baidu.com";"百度")'))
workbook.save('xwlt单元格超链接教学.xls')
八、单元格添加公式
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('飞兔小哥')
worksheet.write(0, 0, 5) # Outputs 5 #第一行第一列5
worksheet.write(0, 1, 2) # Outputs 2 #第一行第二列2
#(A1[5] * A2[2]) 第二行第一列,输出第一行数字乘积
worksheet.write(1, 0, xlwt.Formula('A1*B1'))
#(A1[5] + A2[2]) 第二行第二列,输出第一行数字之和
worksheet.write(1, 1, xlwt.Formula('SUM(A1,B1)'))
workbook.save('xwlt添加公式教学.xls')
九、单元格中输入日期
import xlwt
import datetime
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('飞兔小哥')
style = xlwt.XFStyle()
#其他格式包括: 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
style.num_format_str = 'M/D/YY'
worksheet.write(0, 0, datetime.datetime.now(), style)
workbook.save('xwlt日期格式教学.xlsx')
十、合并行和列
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('飞兔小哥')
worksheet.write_merge(0, 1, 0, 3, '合并从第一行到第二行,第一列到第四列')
#创建字体示例
font = xlwt.Font()
#bold设置为黑体字
font.bold = True
# Create Style
style = xlwt.XFStyle()
# Add Bold Font to Style
style.font = font
worksheet.write_merge(2,3, 0, 3, '合并从第三行到第四行,第一列到第四列', style)
workbook.save('xwlt合并单元格教学.xls')
十一、单元格添加边框
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('飞兔小哥')
# 创建边框对象Create Borders
borders = xlwt.Borders()
#对边框对象进行操作,指定边框上下左右的边框类型为虚线
# 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.left = xlwt.Borders.DASHED
borders.right = xlwt.Borders.DASHED
borders.top = xlwt.Borders.DASHED
borders.bottom = xlwt.Borders.DASHED
#指定上下左右的边框颜色为0x40
borders.left_colour = 0x40
borders.right_colour = 0x40
borders.top_colour = 0x40
borders.bottom_colour = 0x40
# Create Style #创建样式对象
style = xlwt.XFStyle()
# 将设置好的边框对象borders 加到样式对象style中。Add Borders to Style
style.borders = borders
#向单元格第一行第一列写入“单元格内容”,并使用创建好的样式对象style
worksheet.write(0, 0, '单元格内容', style)
workbook.save('xwlt添加边框教学.xls')