Python 处理 Excel 表格,openpyxl常见用法整理
在工作中处理Excel时,可用Python增加处理效率,此处介绍openpyxl库的常见用法:
安装:pip install openpyxl
复制代码
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167# -*- coding = utf-8 -*- # @Time : 2021/12/17 9:04 # @Author : 修理工小刘 # @File : openpyxl_demo.py # @Software: PyCharm import openpyxl # 本例是基于openpyxl的操作excel文件demo class MYOPENPY: def __init__(self): pass # 创建文件 def createfile(self): self.wb = openpyxl.Workbook() # 加载文件 def loadfile(self): self.wb = openpyxl.load_workbook("myexcel.xlsx") # 保存文件 def savefile(self): self.wb.save("myexcel.xlsx") # 打印表单 def printsheet(self): print(self.wb.sheetnames) # 创建表单 def createsheet(self): self.ws = self.wb.create_sheet("mySheet", 0) # 获取表单 def getsheet(self): # 方式1 self.ws = self.wb["mySheet"] # 方式2 已经弃用了,不推荐 # self.ws = self.wb.get_sheet_by_name("mySheet") # 获取单元格 def getcell(self): # 方式1 self.cell = self.ws["A1"] # 方式2 self.cell = self.ws.cell("A1") # 方式3 row行 column列 行列都是从1开始,推荐这种方式 self.cell = self.ws.cell(1, 1) # 填写单元格 def writecell(self): # 方式1 self.ws["A2"] = 10 # 方式2 self.ws.cell(2, 2, value=20) # 获取单元格索引 def getcellidx(self): self.cell = self.ws["C6"] # 获取行标 6 print(self.cell.row) # 获取列标 3 = C print(self.cell.column) # 获取列名 "C" print(self.cell.column_letter) # 获取坐标 "C6" print(self.cell.coordinate) # 获取数据类型 默认n数值,s字符串,d日期 print(self.cell.data_type) # 单元格编码格式 utf-8 print(self.cell.encoding) # 获取多个单元格并打印 def getmorecell(self): self.cellrange = self.ws["A1":"B2"] # 是一个二维元组,元组和列表的区别在于不能修改 print(self.cellrange) # 打印B2的值 print(self.cellrange[1][1].value) # 获取单行或单列 def getlist(self): # 获取列,保存为一维元祖 遍历即可 self.column_A = self.ws["A"] # 获取行,保存为一维元祖 遍历即可 self.row_1 = self.ws[1] # 获取多列,保存为二维元祖 遍历即可 self.column_AB = self.ws["A":"B"] # 获取多行,保存为二维元祖 遍历即可 self.row_1_6 = self.ws[1:6] # 行列遍历 def ergodic(self): # 行遍历 for i in self.ws.rows: print(i) # 行遍历 带范围 for i in self.ws.iter_rows(min_row=1, max_row=3, min_col=1, max_col=4): print(i) # 列遍历 for i in self.ws.columns: print(i) # 列遍历 带范围 for i in self.ws.iter_cols(min_row=1, max_row=3, min_col=1, max_col=4): print(i) # 在尾部插入行 def appendrow(self): self.ws.append([1, 2, 3]) # 其他不常见操作 def otherprocess(self): # 获取当前表单 self.ws = self.wb.active # 表单改名 self.ws.title = "yorSheet" # 遍历值 for v in self.ws.values: print(v) # 删除行列,注意,会自动向前填充 self.ws.delete_rows(3) self.ws.delete_cols(1) # 设置最大行,最大列 self.ws.max_column = 10 self.ws.max_row = 10 # 参考教程 def course(self): course1 = "https://www.imooc.com/article/291433" course2 = "https://blog.csdn.net/weixin_41546513/article/details/109555832" # 例程1 逐行遍历 def demo1(self): wb = openpyxl.Workbook() ws = wb.active ws.title = "test" # 就是正常顺序 for i in range(1, 4): # 表示 1 2 3 for j in range(1, 5): ws.cell(row=i, column=j, value=i+j) # 正常顺序 for row in ws: for cell in row: print(cell.value) # 例程2 逐列遍历 def demo2(self): wb = openpyxl.Workbook() ws = wb.active # 数据写入 for i in range(1, 4): for j in range(1, 5): ws.cell(row=i, column=j, value=str(i)+","+str(j)) # 逐列遍历 for col in ws.iter_cols(min_row=1, max_col=4, max_row=3): for cell in col: print(cell.value) if __name__ == '__main__': print('begin') my = MYOPENPY() my.loadfile() my.getsheet() my.getcellidx() # my.savefile()
最后
以上就是飘逸八宝粥最近收集整理的关于Python 处理 Excel 表格,openpyxl常见用法整理的全部内容,更多相关Python内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复