Python excel 功能扩展库 ——> openpyxl 的基本使用

说明:本文档内容参考自 https://www.cnblogs.com/zeke-python-road/p/8986318.html (作者:关关雎鸠`)的文档
 
  1 from openpyxl import Workbook
  2 
  3 from openpyxl import load_workbook
  4 # 实例化一个操作对象
  5 wb = Workbook()
  6 
  7 # 获取当前活跃状态的sheet
  8 ws = wb.active
  9 
 10 '''
 11 # 基本操作,插入方式按顺序逐行插入
 12 # 单元格内容控制
 13 ws['A1'] = '姓名'
 14 ws['B1'] = '性别'
 15 # 在上文基础上添加整行(上文存在三行,则从第四行开始整行插入)
 16 ws.append(['项目','姓名','时间','报价','备注'])
 17 ws.append([1,2,3])
 18 ws.append([23,34,45,56])
 19 # 单元格内容控制
 20 ws['A2'] = 'jony'
 21 ws['A3'] = 'male'
 22 # 文件保存(必须用绝对路径)
 23 wb.save('/home/ht/fir.xlsx')
 24 '''
 25 
 26 '''
 27 # 创建新的工作区,并输入该区的标签名(标签名,位置)* 位置:该区在标签中的排序
 28 w1 = wb.create_sheet('sheet1')
 29 w2 = wb.create_sheet('sheet2')
 30 w3 = wb.create_sheet('sheet3',0)
 31 
 32 # 这里是修改该区标签名
 33 w1.title = 'sheet-1'
 34 w2.title = 'sheet-2'
 35 w3.title = 'sheet-3'
 36 
 37 w2.append(['ds','hp','wq'])
 38 
 39 # 设置标签的背景色(不是表格的单元格)
 40 w2.sheet_properties.tabColor = "1072BA"
 41 
 42 # 可以通过工作区的title名来获取该sheet对象
 43 wanna = wb.get_sheet_by_name('sheet-2')
 44 
 45 print(wanna)
 46 print(wb.sheetnames)
 47 
 48 # <Worksheet "sheet-2">
 49 # ['sheet-3', 'Sheet', 'sheet-1', 'sheet-2']
 50 
 51 wb['sheet-3'].append([1,2,3,4,5])
 52 
 53 # 复制工作区,新的工作区的默认命名为sheet-3 Copy
 54 new_3 = wb.copy_worksheet(w3)
 55 # 复制品重命名
 56 new_3.title = 'new'
 57 wb.save('/home/ht/mul_sheet.xlsx')
 58 '''
 59 
 60 '''
 61 sh1 = wb.active
 62 sh2 = wb.create_sheet('sheet-cell')
 63 
 64 # 单表格坐标数值输入
 65 sh2['A1'] = 'aaa插入内容'
 66 # 单元格坐标接受小写
 67 sh2['d4'] = '表格小写'
 68 # 单元格行列值坐标输入
 69 cell1 = sh2.cell(row=3,column=2,value='三行二列')
 70 cell2 = sh2.cell(3,4,'三行四列')
 71 print(cell1.value)
 72 # 三行二列
 73 wb.save('/home/ht/sheet-cell.xlsx')
 74 '''
 75 
 76 '''
 77 # 批量获取单元格
 78 mul_cell = wb.active
 79 mul_cell.append(['a1','b1','c1','d1'])
 80 mul_cell.append(['a2','b2','c2','d2'])
 81 mul_cell.append(['a3','b3','c3','d3'])
 82 
 83 # 获取A列所有单元格
 84 print(mul_cell['a'])
 85 # (<Cell 'Sheet'.A1>,
 86 # <Cell 'Sheet'.A2>,
 87 # <Cell 'Sheet'.A3>)
 88 
 89 # 获取 BCD 三列所有单元格
 90 print(mul_cell['b:d'])
 91 # ((<Cell 'Sheet'.B1>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.B3>),
 92 # (<Cell 'Sheet'.C1>, <Cell 'Sheet'.C2>, <Cell 'Sheet'.C3>),
 93 # (<Cell 'Sheet'.D1>, <Cell 'Sheet'.D2>, <Cell 'Sheet'.D3>))
 94 
 95 # 获取第 2到3 行所有单元格
 96 print(mul_cell[2:3])
 97 # ((<Cell 'Sheet'.A2>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.C2>, <Cell 'Sheet'.D2>),
 98 # (<Cell 'Sheet'.A3>, <Cell 'Sheet'.B3>, <Cell 'Sheet'.C3>, <Cell 'Sheet'.D3>))
 99 
100 # iter_rows 方法,设定参数,获取二行三列区块内的所有单元格(获取基本单位为行,再从行内获取单元格)
101 for r in mul_cell.iter_rows(min_row=1,max_row=2,min_col=1,max_col=3):
102     print(r)
103     for c in r:
104         print(c,c.value)
105 
106 # (<Cell 'Sheet'.A1>, <Cell 'Sheet'.B1>, <Cell 'Sheet'.C1>)
107 # <Cell 'Sheet'.A1> a1
108 # <Cell 'Sheet'.B1> b1
109 # <Cell 'Sheet'.C1> c1
110 # (<Cell 'Sheet'.A2>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.C2>)
111 # <Cell 'Sheet'.A2> a2
112 # <Cell 'Sheet'.B2> b2
113 # <Cell 'Sheet'.C2> c2
114 
115 # iter_rows 方法,设定参数,获取二行三列区块内的所有单元格(获取基本单位为列,再从列内获取单元格)
116 for r in mul_cell.iter_cols(min_row=1,max_row=2,min_col=1,max_col=3):
117     print(r)
118 
119 # (<Cell 'Sheet'.A1>, <Cell 'Sheet'.A2>)
120 # (<Cell 'Sheet'.B1>, <Cell 'Sheet'.B2>)
121 # (<Cell 'Sheet'.C1>, <Cell 'Sheet'.C2>)
122 
123 # 获取所有行
124 for r in mul_cell.rows:
125     print(r)
126 # (<Cell 'Sheet'.A1>, <Cell 'Sheet'.B1>, <Cell 'Sheet'.C1>, <Cell 'Sheet'.D1>)
127 # (<Cell 'Sheet'.A2>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.C2>, <Cell 'Sheet'.D2>)
128 # (<Cell 'Sheet'.A3>, <Cell 'Sheet'.B3>, <Cell 'Sheet'.C3>, <Cell 'Sheet'.D3>)
129 
130 print(mul_cell.rows)
131 # <generator object Worksheet._cells_by_row at 0x7f4c615312b0>
132 
133 # 获取所有列
134 print(mul_cell.columns)
135 # <generator object Worksheet._cells_by_col at 0x7f4c615312b0>
136 wb.save('/home/ht/mul_cell.xlsx')
137 '''
138 
139 '''
140 from openpyxl import load_workbook
141 
142 # 操作文件
143 read_sheet = load_workbook('/home/ht/mul_sheet.xlsx')
144 
145 read_sheet.guess_types = True
146 
147 current = read_sheet.active
148 current['e2'] = '65%'
149 
150 read_sheet.save('/home/ht/mul_sheet.xlsx')
151 '''
152 
153 '''
154 from openpyxl import load_workbook
155 wsheet = load_workbook('/home/ht/mul_cell.xlsx')
156 
157 # sheet 对象不存在脚标,只能通过坐标获取,wsheet.active.rows[1]是不能获取第二行的!
158 print(wsheet.active['a'][1].value)
159 print(wsheet.active['2'][1].value)
160 # a2
161 # b2
162 '''
163 
164 ''' 单元格分割合并
165 
166 deal_cell = load_workbook('/home/ht/sum_sheet.xlsx')
167 sheet = deal_cell.active
168 
169 # 合并第一行1到4单元格(a1,b1,c1,d1)
170 sheet.merge_cells('a1:d1')
171 
172 # 将 第一行 a1 到 d1 位置的单元格分离出来
173 sheet.unmerge_cells('a1:d1')
174 
175 # 采用区块的方式合并或分割操作
176 sheet.merge_cells(start_row=1,end_row=3,start_column=4,end_column=6)
177 
178 deal_cell.save('/home/ht/sum_sheet.xlsx')
179 '''
180 
181 ''' 单元格插入图片
182 
183 # 需要安装PIL图片包
184 from openpyxl.drawing.image import Image
185 
186 img = Image('/home/ht/qcode.png')
187 ws.add_image(img,'F3')      # 这里图片填充的位置坐标必须使用大写字母
188 wb.save('/home/ht/addimg.xlsx')
189 '''
原文地址:https://www.cnblogs.com/haiton/p/10522806.html