一、xlrd获取文件内容
import xlrd
1.打开Excel文件
ex = xlrd.open_workbook('files/data1.xls')
2.获取所有工作表的表名
工作簿.sheet_names()
sheet = ex.sheet_names()
print(sheet)
3.获取工作表
3.1 获取所有的工作表:工作簿.sheets()
all_sheet = ex.sheets()
print(all_sheet)
3.2 获取一个工作表
工作簿.sheet_by_name(‘表名’)
stu_sheet = ex.sheet_by_name('students')
print(stu_sheet)
工作簿.sheet_by_index(下标值)
tea_sheet = ex.sheet_by_index(1)
print(tea_sheet)
4.获取最大行数、最大列数
工作表.nrows
工作表.ncols
max_r = stu_sheet.nrows
max_c = stu_sheet.ncols
print(max_r, max_c)
5.按行或按列获取数据
5.1 按行获取对应的数据
工作表.row_values(行下标) - 获取指定行对应的所有数据,行下标从0开始
工作表.row_values(行下标, 起始列下标) - 获取指定行指定列后面的所有数据,行下标与列下标从0开始
工作表.row_values(行下标, 起始列下标, 终点列下标) - 获取指定行指定范围内的所有数据,行下标、列下标从0开始
r = stu_sheet.row_values(2, 3, 4)
print(r)
5.2 按列获取对应的数据
工作表.col_values(列下标) - 获取指定列对应的所有数据,行下标从0开始
工作表.col_values(列下标, 起始行下标) - 获取指定列指定列后面的所有数据,行下标与列下标从0开始
工作表.col_values(列下标, 起始行下标, 终点行下标) - 获取指定列指定范围内的所有数据,行下标、列下标从0开始
r0 = stu_sheet.col_values(3, 1)
print(r0)
练习1:一行一行的获取整个学生表中所有的数据
for r in range(max_r):
r1 = stu_sheet.row_values(r, 0)
print(r1)
练习2:一列一列的获取所有学生的信息
for c in range(max_c):
r2 = stu_sheet.col_values(c, 0)
print(r2)
6.获取单元格
6.1 单元格 = 工作表.cell(行下标, 列下标) - 获取指定位置对应的单元格对象
6.2 单元格.value - 获取该单元格的数据
print(stu_sheet.cell(1, 0).value)
6.3 工作表.row(行下标)[某一个的下标] - 获取指定行中所有的单元格信息
re = stu_sheet.row(0)[2]
print(re)
6.4 工作表.col(列下标)[某一个的下标] - 获取指定列中所有的单元格信息
re = stu_sheet.col(0)[2]
print(re)
二、xlwt对文件的写操作
import xlwt
1.创建xls类型的Excel文件(创建工作簿)
注意:创建xls类型的Excel的时候不会自动创建默认工作表
ex = xlwt.Workbook()
2.为新建的工作簿添加表
2.1 工作簿.add_sheet(‘表名’) - 在工作簿中添加指定名字对应的的工作表(这个表中的单元格默认不支持重写cell_overwrite_ok=Flase)
sheet1 = ex.add_sheet('数据1')
sheet2 = ex.add_sheet('数据2', cell_overwrite_ok=True)
3.写入数据
工作表.write(行下标, 列下标, ‘数据’) - 将数据写入到指定的工作表中指定的位置
sheet1.write(0, 0, '姓名1')
sheet2.write(0, 0, '姓名1')
sheet2.write(0, 0, '姓名2')
ex.save('files/example1.xls')
三、xlwt修改单元格样式
import xlwt
ex = xlwt.Workbook()
sheet = ex.add_sheet('style', cell_overwrite_ok=True)
设置单元格的样式
1.创建样式对象
style1 = xlwt.XFStyle()
2.添加字体样式
font1 = xlwt.Font()
font1.name = '宋体'
font1.bold = True
font1.height = 15 * 15
>print(xlwt.Style.colour_map) 颜色库
font1.colour_index = 12
style1.font = font1
3.添加边框样式
b1 = xlwt.Borders()
b1.top = 10
b1.top_colour = 16
b1.left = 10
b1.left_colour = 16
b1.bottom = 10
b1.bottom_colour = 16
b1.right = 10
b1.right_colour = 16
style1.borders = b1
4.添加对齐样式
style2 = xlwt.XFStyle()
al2 = xlwt.Alignment()
al2.horz = xlwt.Alignment.HORZ_CENTER
al2.vert = xlwt.Alignment.VERT_CENTER
# al1.rota = 90
style2.alignment = al2
5.单元格填充
style2 = xlwt.XFStyle()
p2 = xlwt.Pattern()
p2.pattern = xlwt.Pattern.SOLID_PATTERN
p2.pattern_fore_colour = 13
style2.pattern = p2
6.合并单元格
工作表。write_merge(行下标1, 行下标2, 列下标1, 列下标2, 数据, 样式)
sheet.write_merge(12, 22, 2, 7, 'hello', style=style2)
7.设置单元格的宽度和高度
7.1 设置某一列的宽度
sheet.col(0).width = 20*256
7.2 设置行的高度
需要先将行高匹配关闭
sheet.row(0).height_mismatch = True
sheet.row(0).height = 10*60
sheet.write(0, 0, 'Name', style=style1)
sheet.write(1, 1, 'Gender', style=style2)
ex.save('files/example2.xls')
四、xlwt颜色对照表练习
import xlwt
ex = xlwt.Workbook()
sheet = ex.add_sheet('xls颜色对照表', cell_overwrite_ok=True)
style1 = xlwt.XFStyle()
font1 = xlwt.Font()
font1.bold = True
font1.height = 14*14
style1.font = font1
al1 = xlwt.Alignment()
al1.horz = xlwt.Alignment.HORZ_CENTER
al1.vert = xlwt.Alignment.VERT_CENTER
style1.alignment = al1
style3 = xlwt.XFStyle()
al3 = xlwt.Alignment()
al3.horz = xlwt.Alignment.HORZ_CENTER
al3.vert = xlwt.Alignment.VERT_CENTER
style3.alignment = al3
sheet.write(0, 0, '颜色', style=style1)
sheet.write(0, 1, '颜色单词', style=style1)
sheet.write(0, 2, '颜色值', style=style1)
sheet.row(0).height_mismatch = True
sheet.row(0).height = 6 * 60
color = xlwt.Style.colour_map
colors_d = []
for x in color:
colors_d.append(color[x])
colors_ds = sorted(colors_d)
colors_y = []
for x in colors_ds:
for y in color:
if x == color[y]:
colors_y.append(y)
colors_z = []
for z in colors_y:
if z not in colors_z:
colors_z.append(z)
r = 1
for x in colors_ds:
style2 = xlwt.XFStyle()
p = xlwt.Pattern()
p.pattern = xlwt.Pattern.SOLID_PATTERN
p.pattern_fore_colour = x
style2.pattern = p
sheet.row(r).height_mismatch = True
sheet.row(r).height = 6 * 60
sheet.write(r, 0, None, style=style2)
sheet.write(r, 2, x, style=style3)
r += 1
r2 = 1
for y in colors_z:
sheet.write(r2, 1, y, style=style3)
r2 += 1
for c in range(3):
sheet.col(c).width = 15 * 256
ex.save('files/Python(xls)颜色对照表.xls')
五、xlutils的用法
xlrd只能对Excel文件进行读取操作,而且只能用通过xlrd打开的Excel文件(xlrd无法直接操作xlwt创建的工作簿)
xlwt只能创建新的Excel,无法获取已经存在的Excel文件
实验结果:xlutils只能让已经存在的Excel可添加新的表的写操作,不能直接修改表中的数据
import xlrd, xlwt
from xlutils.copy import copy
1.使用xlrd打开已经存在的工作簿对象(得到一个只读的工作簿)
ex = xlrd.open_workbook('files/data1.xls')
2.将只读的工作簿转换成可写的工作簿
ex1 = copy(ex)
sheet1 = ex1.add_sheet('data')
ex1.save('files/data1.xls')
六、xls格式文件作业
import xlrd
import xlwt
from xlutils.copy import copy
ex = xlrd.open_workbook('files/作业数据.xls')
ex1 = copy(ex)
sheet1 = ex1.add_sheet('筛选结果报告1', cell_overwrite_ok=True)
sheet = ex.sheet_by_name('原始数据')
max_r = sheet.nrows
max_c = sheet.ncols
for r in range(max_r):
s = sheet.row_values(r, 0)
count = 0
for x in s:
sheet1.write(r, count, x)
count += 1
style1 = xlwt.XFStyle()
p1 = xlwt.Pattern()
p1.pattern = xlwt.Pattern.SOLID_PATTERN
p1.pattern_fore_colour = 47
style1.pattern = p1
style2 = xlwt.XFStyle()
font2 = xlwt.Font()
font2.name = '黑体'
font2.height = 15 * 15
font2.colour_index = 10
style2.font = font2
style3 = xlwt.XFStyle()
al3 = xlwt.Alignment()
al3.horz = xlwt.Alignment.HORZ_CENTER
al3.vert = xlwt.Alignment.VERT_CENTER
style1.alignment = al3
style2.alignment = al3
style3.alignment = al3
style4 = xlwt.XFStyle()
font4 = xlwt.Font()
font4.bold = True
style4.font = font4
style4.alignment = al3
style5 = xlwt.XFStyle()
font5 = xlwt.Font()
font5.height = 16 * 16
font5.bold = True
style5.font = font5
style5.alignment = al3
s1 = ''
for r in range(max_r):
sheet1.row(r).height_mismatch = True
sheet1.row(r).height = 5 * 60
for c in range(max_c):
s1 = sheet.cell(r, c).value
if r == 0:
sheet1.write(0, c, s1, style=style5)
elif c == 0:
sheet1.write(r, 0, s1, style=style4)
else:
sheet1.write(r, c, s1, style=style3)
sheet1.col(c).width = 20 * 256
for r in range(1, max_r):
for c in range(1, max_c):
s1 = sheet.cell(r, c).value
sheet1.write(r, c, s1, style=style3)
if 0 < s1 < 60:
sheet1.write(r, c, s1, style=style1)
elif s1 == 0:
sheet1.write(r, c, '补考', style=style2)
ex1.save('files/作业数据.xls')