关于python:py读写修改Excel之xlrdxlwtxlutils

31次阅读

共计 2827 个字符,预计需要花费 8 分钟才能阅读完成。

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))

正文完
 0