py读写修改Excel之xlrd&xlwt&xlutils

Posted Gendan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了py读写修改Excel之xlrd&xlwt&xlutils相关的知识,希望对你有一定的参考价值。

from datetime import datetime
import xlwt
font0 = xlwt.Font()

font0.name = \'Times New Roman\' # 适用于字母或数字

font0.name = \'宋体\' # 适用于中文,适配字体或者不指定字体才能体现出指定的颜色

font0.colour_index = 1 # 白色

font0.colour_index = 2 # 红色

font0.colour_index = 3 # 绿色

font0.colour_index = 4 # 蓝色

font0.colour_index = 5 # 黄色

font0.colour_index = 6 # 紫色

font0.colour_index = 7 # 青色

font0.colour_index = 8 # 黑色,比默认加黑,不加粗

font0.colour_index = 4 # 蓝色
font0.bold = True
style0 = xlwt.XFStyle()
style0.font = font0

创建样式对象:日期格式

style1 = xlwt.XFStyle()
style1.num_format_str = \'YYYY-MM-DD\'

创建样式对象:字体居中对齐

style2 = xlwt.XFStyle()
al = xlwt.Alignment()
al.horz = 0x02 # 设置水平居中
al.vert = 0x01 # 设置垂直居中
style2.alignment = al

创建样式对象,设置日期格式与字体居中对齐

style3 = xlwt.XFStyle()
style3.num_format_str = \'YYYY-MM-DD\'
style3.alignment = al

创建样式对象,设置字体居中 且 设置字体颜色

style4 = xlwt.XFStyle()
style4.alignment = al
style4.font = font0
now_time = datetime.now().strftime(\'%Y-%m-%d %X\')
date_time = datetime.now().strftime(\'%Y-%m-%d\')

创建表格

wb = xlwt.Workbook()

新建一个名为 Score Sheet 的表单页

score_sheet = wb.add_sheet(\'Score Sheet\')

新建一个名为 Record Test Sheet 的表单页

record_test_sheet = wb.add_sheet(\'Record Test Sheet\')

1、写入 Score Sheet 表单

设置 表头, 第一个参数是行,第二个参数是列

score_sheet.write(0, 0, \'时间\', style2)
score_sheet.write(0, 1, \'班级\', style2)
score_sheet.write(0, 2, \'姓名\', style2)
score_sheet.write(0, 3, \'语文\', style2)
score_sheet.write(0, 4, \'数学\', style2)
score_sheet.write(0, 5, \'英语\', style2)
score_sheet.write(0, 6, \'理综\', style2)
score_sheet.write(0, 7, \'总分\', style4)

按照位置添加数据

score_sheet.write(1, 0, datetime.now(), style3)
score_sheet.write(1, 1, \'高三三班\', style2)
score_sheet.write(1, 2, \'桑岩\', style2)
score_sheet.write(1, 3, 132, style2)
score_sheet.write(1, 4, 150, style2)
score_sheet.write(1, 5, 140, style2)
score_sheet.write(1, 6, 货币代码290, style2)
score_sheet.write(1, 7, xlwt.Formula("D2+E2+F2+G2"), style2)
score_sheet.write(2, 0, datetime.now(), style3)
score_sheet.write(2, 1, \'高三三班\', style2)
score_sheet.write(2, 2, \'项天骐\', style2)
score_sheet.write(2, 3, 140, style2)
score_sheet.write(2, 4, 150, style2)
score_sheet.write(2, 5, 132, style2)
score_sheet.write(2, 6, 280, style2)
score_sheet.write(2, 7, xlwt.Formula("D3+E3+F3+G3"), style2)
score_sheet.write(3, 0, datetime.now(), style3)
score_sheet.write(3, 1, \'高三三班\', style2)
score_sheet.write(3, 2, \'向淮南\', style2)
score_sheet.write(3, 3, 135, style2)
score_sheet.write(3, 4, 150, style2)
score_sheet.write(3, 5, 145, style2)
score_sheet.write(3, 6, 270, style2)
score_sheet.write(3, 7, xlwt.Formula("D4+E4+F4+G4"), style2)

2、写入 Record Test Sheet 表单

record_test_sheet.write(0, 0, \'时间\')
record_test_sheet.write(0, 1, \'学科\', style1)
record_test_sheet.write(0, 2, \'成绩\', style1)
record_test_sheet.write(1, 0, datetime.now(), style1)
record_test_sheet.write(1, 1, \'语文\', style2)
record_test_sheet.write(1, 2, 80)
record_test_sheet.write(2, 0, datetime.now(), style3)
record_test_sheet.write(2, 1, \'数学\', style2)
record_test_sheet.write(2, 2, 99)
record_test_sheet.write(3, 0, now_time, style2)
record_test_sheet.write(3, 1, \'英语\', style2)
record_test_sheet.write(3, 2, 98)

保存表格,这里应该是覆盖写,注意每次都是覆盖所有表单内容,建议每次生成的表单加上时间版本区分

wb.save(\'example.xls\')

wb.save(\'example-{0}.xls\'.format(date_time))

以上是关于py读写修改Excel之xlrd&xlwt&xlutils的主要内容,如果未能解决你的问题,请参考以下文章

python读写Excel方法(xlwt和xlrd)

python读写Excel文件--使用xlrd模块读取,xlwt模块写入

Python在python中使用xlrd和xlwt读写Excel

Python在python中使用xlrd和xlwt读写Excel

Python在python中使用xlrd和xlwt读写Excel

Python在python中使用xlrd和xlwt读写Excel