Day8-xlrd和xlwt操作Excel文件作业
import xlwt, xlrd
from xlutils.copy import copy
wb1 = xlrd.open_workbook('files/副本作业数据.xls')
wb2 = copy(wb1)
sheet = wb2.add_sheet('sheet', cell_overwrite_ok=True)
b1 = xlwt.Borders()
b1.bottom = b1.top = b1.left = b1.right = 1
b1.bottom_colour = b1.top_colour = b1.left_colour = b1.right_colour = 8
al1 = xlwt.Alignment()
al1.vert = xlwt.Alignment.VERT_CENTER
al1.horz = xlwt.Alignment.HORZ_CENTER
style1 = xlwt.XFStyle()
font1 = xlwt.Font()
font1.name = '黑体'
font1.bold = True
font1.height = 15*15
font1.colour_index = 8
style1.font = font1
style1.borders = b1
style1.alignment = al1
p2 = xlwt.Pattern()
p2.pattern = xlwt.Pattern.SOLID_PATTERN
p2.pattern_fore_colour = 52
style1.pattern = p2
style2 = xlwt.XFStyle()
font2 = xlwt.Font()
font2.colour_index = 28
style2.font = font2
style2.borders = b1
style2.alignment = al1
style3 = xlwt.XFStyle()
font3 = xlwt.Font()
font3.colour_index = 10
style3.font = font3
style3.borders = b1
style3.alignment = al1
style4 = xlwt.XFStyle()
p1 = xlwt.Pattern()
p1.pattern = xlwt.Pattern.SOLID_PATTERN
p1.pattern_fore_colour = 47
style4.pattern = p1
style4.borders = b1
style4.alignment = al1
style5 = xlwt.XFStyle()
style5.borders = b1
style5.alignment = al1
sheet_wb1 = wb1.sheet_by_name('原始数据')
list_key = sheet_wb1.row_values(0)
for col in range(len(list_key)):
sheet.write(0, col, list_key[col], style=style1)
sheet.row(0).height_mismatch = True
sheet.row(0).height = 10*60
for col in range(len(list_key)):
sheet.col(col).width = 25 * 256
list_name = sheet_wb1.col_values(0, 1, 19)
for row in range(1, len(list_name)+1):
sheet.write(row, 0, list_name[row-1], style=style2)
for col in range(1, len(list_key)):
scores = sheet_wb1.col_values(col, 1, 19)
for row in range(1, len(scores) + 1):
if sheet_wb1.cell(row, col).value < 60:
sheet.write(row, col, scores[row - 1], style=style4)
if sheet_wb1.cell(row, col).value == 0:
sheet.write(row, col, '补考', style=style3)
else:
sheet.write(row, col, scores[row - 1], style=style5)
wb2.save('files/副本作业数据.xls')
![结果](https://i-blog.csdnimg.cn/blog_migrate/45393540b92f0d06cc5fb36f5dae3e9e.png)