我是靠谱客的博主 平常大树,这篇文章主要介绍利用xrld来获取excle的相关知识工程导入:import xlrd练习xlutils可以对已经存在的Excel文件进行写操作,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
# 获取Excel文件的相关内容的方法

工程导入:import xlrd

复制代码
1
2
3
4
# openpyxl操作xlrd
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 1. 打开Excel文件获取工作簿对象(Excel文件只能是xls文件) wb = xlrd.open_workbook('files/data1.xls') # 2. 获取工作表 # 1)获取所有工作表的表名 print(wb.sheet_names()) # 2) 获取工作表 # a.获取所有的工作表 # 工作簿对象.sheets() all_sheet = wb.sheets() print(all_sheet) # [Sheet 0:<students>, Sheet 1:<teacher>] # b.获取指定下标对应的工作表 # 工作簿对象.sheet_by_index(下标) print(wb.sheet_by_index(0)) # Sheet 0:<students> print(wb.sheet_by_index(1)) # Sheet 1:<teacher> # c.获取指定名字对应的工作表 # 工作簿对象.sheet_by_name(表名) students = wb.sheet_by_name('students') # 3.获取表中的相关信息 # 1)获取表中数据部分的最大行数和最大列数 # 表对象.nrows - 最大行数 # 表对象.ncols - 最大列数 print(students.nrows, students.ncols) # 2)按行或者按列获取数据 print(students.row_values(0)) # ['姓名', '性别', '年龄', '分数'] print(students.col_values(2)) # ['年龄', 20.0, 25.0, 22.0, 30.0] print(students.col_values(0)) # ['姓名', '小明', '张三', '小花', '老王'] print(students.col_values(0, start_rowx=1)) # ['小明', '张三', '小花', '老王'] print(students.col_values(0, start_rowx=1, end_rowx=4)) # ['小明', '张三', '小花']

练习

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 练习1:一行一行的获取整个表中所有的数据 for x in range(students.nrows): print(students.row_values(x)) print('---------------------------------------华丽的分割线------------------------------------') # 练习2:一列一列的获取整个表中所有的数据 for x in range(students.ncols): print(students.col_values(x)) print('---------------------------------------华丽的分割线------------------------------------') # 3)获取单元格对象 # a.工作表.cell(行下标, 列下标) - 获取指定位置对应的单元格 # 单元格对象.value print(students.cell(0, 0)) # text:'姓名' print(students.cell(0, 0).value) # 姓名 # b.工作表.row(行下标) - 获取指定行中所有有数据的单元格对象 print(students.row(1)) # [text:'小明', text:'男', number:20.0, number:99.0] print(students.row(1)[-1]) # number:99.0 # c.工作表.col(列下标) - 获取指定列中所有有数据的单元格对象 print(students.col(2)) # [text:'年龄', number:20.0, number:25.0, number:22.0, number:30.0]

xlutils可以对已经存在的Excel文件进行写操作

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import xlrd from xlutils.copy import copy # 1. 打开指定的Excel文件 r_wb = xlrd.open_workbook('files/data1.xls', formatting_info=True) # 2.对打开的工作簿对象进行拷贝,得到一个可写的工作簿 w_wb = copy(r_wb) # 3.在新的工作表中写数据 # sheet1 = w_wb.add_sheet('班级') # sheet1.write(0, 0, '班级名称') # 4.在已经存在的工作表中写数据(将students中'小明', 改成'XiaoMing') sheet2_n = w_wb.add_sheet('students2', cell_overwrite_ok=True) sheet2_o = r_wb.sheet_by_index(0) for r in range(sheet2_o.nrows): r_data = sheet2_o.row_values(r) col = 0 for x in r_data: sheet2_n.write(r, col, x) col += 1 sheet2_n.write(1, 0, 'XiaoMing') w_wb.save('files/data1.xls')
复制代码
1
2
3
4
# 重点练习:创建颜色对应表

具体操作:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import xlwt # 练习1:新建一个Excel文件,创建一个颜色对应表 # result = xlwt.Style.colour_map # print(result) # print('---------------------------------------华丽的分割线------------------------------------') # for x in result: # print(x, result[x]) # 1. 创建工作簿和工作表 wb = xlwt.Workbook() sheet = wb.add_sheet('颜色对照表') # 2. 添加颜色信息 # 1)获取所有的颜色值 color_map = xlwt.Style.colour_map # 写入第一行的数据 sheet.write(0, 0, '颜色') sheet.write(0, 1, '颜色单词') sheet.write(0, 2, '颜色值') # 3)将颜色信息写入到单元格中 row_index = 1 for x in color_map: # 拿到颜色值 color_value = color_map[x] print(x, color_value) # 创建对应的填充对象 style = xlwt.XFStyle() fill = xlwt.Pattern() fill.pattern = xlwt.Pattern.SOLID_PATTERN fill.pattern_fore_colour = color_value style.pattern = fill # 设置对应单元格的样式 sheet.write(row_index, 0, '', style=style) sheet.write(row_index, 1, x) sheet.write(row_index, 2, color_value) row_index += 1 wb.save('files/颜色值.xls')

最后

以上就是平常大树最近收集整理的关于利用xrld来获取excle的相关知识工程导入:import xlrd练习xlutils可以对已经存在的Excel文件进行写操作的全部内容,更多相关利用xrld来获取excle的相关知识工程导入:import内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(82)

评论列表共有 0 条评论

立即
投稿
返回
顶部