这篇文章主要介绍了python使用openpyxl操作excel的方法步骤,详细的介绍了openpyxl的常用属性以及操作excel的方法,感兴趣的可以了解一下
一 前言
放大招了,学完这篇openpyxl第三方库,读者将会懂得如何灵活的读取excel数据,如何创建excel工作表;更新工作表,删除工作表;是不是感觉很强大,留下赞赞吧!!
二 openpyxl常用属性函数
三 读取excel
读取整体流程如下
3.1 准备工作
创建一个名为zszxz.xlsx excel文件;其中标签sheet名为zszxz; 列A B C 如下;
id num name 1 100 zszxz 2 101 smile 3 102 kitty 4 103 wolf 5 104 cloud 6 105 water
3.2 安装
支持操作excel和图像
pip install openpyxl pip install pillow
3.3 获取所有标签页名称
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz.xlsx' # 加载工作本 workbook = openpyxl.load_workbook(path) # 获得所有标签页 sheet_names = workbook.get_sheet_names() print(sheet_names)
输出
['zszxz']
正确的获取方式
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz.xlsx' # 加载工作本 workbook = openpyxl.load_workbook(path) # 获得所有标签页名称 print(workbook.sheetnames)
3.4 获取指定标签页对象
不建议通过方法获得标签页对象,建议通过属性方式获取;
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz.xlsx' # 加载工作本 workbook = openpyxl.load_workbook(path) # 获得指定的sheet sheet = workbook.get_sheet_by_name('zszxz') print(sheet)
<Worksheet "zszxz">
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz.xlsx' # 加载工作本 workbook = openpyxl.load_workbook(path) # 获取sheet对象 sheet = workbook['zszxz'] print(sheet)
当然读者也可以通过循环工作本方式获取sheet,不赘述;
3.5 复制sheet
在已有的sheet上可以进行复制一个副本;
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz.xlsx' # 加载工作本 workbook = openpyxl.load_workbook(path) # 获取sheet对象 sheet = workbook['zszxz'] # 复制sheet cp_sheet = workbook.copy_worksheet(sheet) print(cp_sheet)
<Worksheet "zszxz Copy">
3.5 获取指定一个单元格对象
指定获取A1单元格对象
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz.xlsx' # 加载工作本 workbook = openpyxl.load_workbook(path) # 获取sheet对象 sheet = workbook['zszxz'] # 获取指定单元格 cell = sheet['A1'] print(cell)
<Cell 'zszxz'.A1>
方式二
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz.xlsx' # 加载工作本 workbook = openpyxl.load_workbook(path) # 获取sheet对象 sheet = workbook['zszxz'] # 获取指定单元格 cell = sheet.cell(row=1, column=1) print(cell)
3.6 访问多个单元格
访问单元格获取单元格对象;
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz.xlsx' # 加载工作本 workbook = openpyxl.load_workbook(path) # 获取sheet对象 sheet = workbook['zszxz'] cells_range = sheet['A1':'C1'] for cells in cells_range: for cell in cells: print(cell)
<Cell 'zszxz'.A1> <Cell 'zszxz'.B1> <Cell 'zszxz'.C1>
<Cell 'zszxz'.B1>
<Cell 'zszxz'.C1>
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz.xlsx' # 加载工作本 workbook = openpyxl.load_workbook(path) # 获取sheet对象 sheet = workbook['zszxz'] # 获取指定范围的行 row = sheet.iter_rows(min_row=1, max_col=3, max_row=1) for cell in row: print(cell)
(<Cell 'zszxz'.A1>, <Cell 'zszxz'.B1>, <Cell 'zszxz'.C1>)
sheet.iter_cols(min_row, max_col, max_row)通用的道理,不赘述
3.7 获取全部行
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz.xlsx' # 加载工作本 workbook = openpyxl.load_workbook(path) # 获取sheet对象 sheet = workbook['zszxz'] # 获取所有行 for row in sheet.rows: print(row)
(<Cell 'zszxz'.A1>, <Cell 'zszxz'.B1>, <Cell 'zszxz'.C1>) (<Cell 'zszxz'.A2>, <Cell 'zszxz'.B2>, <Cell 'zszxz'.C2>) (<Cell 'zszxz'.A3>, <Cell 'zszxz'.B3>, <Cell 'zszxz'.C3>) (<Cell 'zszxz'.A4>, <Cell 'zszxz'.B4>, <Cell 'zszxz'.C4>) (<Cell 'zszxz'.A5>, <Cell 'zszxz'.B5>, <Cell 'zszxz'.C5>) (<Cell 'zszxz'.A6>, <Cell 'zszxz'.B6>, <Cell 'zszxz'.C6>) (<Cell 'zszxz'.A7>, <Cell 'zszxz'.B7>, <Cell 'zszxz'.C7>)
同理 sheet.columns获取所有列不再赘述
3.8 获取值
cell.value获取属性值
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz.xlsx' # 加载工作本 workbook = openpyxl.load_workbook(path) # 获取sheet对象 sheet = workbook['zszxz'] cells_range = sheet['A1':'C1'] for cells in cells_range: for cell in cells: # 获取属性值 print(cell.value)
id num name
四 写入 excel
4.1 写入文本
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz_write.xlsx' # 创建一个工作本 workbook = openpyxl.Workbook() # 创建一个 sheet sheet = workbook.create_sheet('zszxz') # 写入文本形式 sheet['A1'] = 'zszxz666' # 读取 print(sheet['A1'].value) # 保存 workbook.save(path)
zszxz666
4.2 写入数字
# -*- coding: utf-8 -*- import openpyxl import datetime path = r'C:\mydata\generator\py\zszxz_write.xlsx' # 创建一个工作本 workbook = openpyxl.Workbook() # 创建一个 sheet sheet = workbook.create_sheet('zszxz') # 写入值数字形式 sheet['A2'] = datetime.datetime(2020, 10, 15) print(sheet['A2'].value) # 保存 workbook.save(path) 输出
>
2020-10-15 00:00:00
4.3 写入公式
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz_write.xlsx' # 创建一个工作本 workbook = openpyxl.Workbook() # 创建一个 sheet sheet = workbook.create_sheet('zszxz') # 写入公式 sheet['A3'] = '=SUM(1, 1)' print(sheet['A3'].value) # 保存 workbook.save(path)
4.5 取消合并单元格
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz_write.xlsx' # 创建一个工作本 workbook = openpyxl.Workbook() # 创建一个 sheet sheet = workbook.create_sheet('zszxz') # 取消合并单元格 sheet.unmerge_cells('A2:D2') # 保存 workbook.save(path)
4.5 插入图片
# -*- coding: utf-8 -*- import openpyxl from openpyxl.drawing.image import Image path = r'C:\mydata\generator\py\zszxz_write.xlsx' # 创建一个工作本 workbook = openpyxl.Workbook() # 创建一个 sheet sheet = workbook.create_sheet('zszxz') # 设置图像 img = Image(r'C:\mydata\generator\py\main.jpg') # 设置图像单元格说明 sheet['A1'] = 'you are my angel' # 插入图片 sheet.add_image(img, 'A1') # 保存 workbook.save(path)
结果如下:
4.6 隐藏轮廓
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz_write.xlsx' wb = openpyxl.Workbook() ws = wb.create_sheet() # 隐藏行 A-B ws.column_dimensions.group('A', 'B', hidden=True) # 隐藏 列 1 -5 ws.row_dimensions.group(1, 5, hidden=True) wb.save(path)
4.7 设置行高列高
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz_write.xlsx' # 创建一个工作本 workbook = openpyxl.Workbook() # 创建一个 sheet sheet = workbook.create_sheet('zszxz') # 写入文本形式 sheet['A1'] = 'zszxz666' # 设置行高 sheet.row_dimensions[1].height = 50 # 设置列高 sheet.column_dimensions['A'].width = 30 workbook.save(path)
五 删除sheet
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz_write.xlsx' # 加载工作本 workbook = openpyxl.load_workbook(path) # 获得sheet sheet = workbook['Sheet'] # 移除sheet workbook.remove(sheet) # 保存 workbook.save(path)
六 更新工作表
# -*- coding: utf-8 -*- import openpyxl path = r'C:\mydata\generator\py\zszxz_write.xlsx' # 加载工作本 workbook = openpyxl.load_workbook(path) # 获得sheet sheet = workbook['zszxz'] # 获得值 val = sheet['A1'].value print(val) # 重新赋值 new_val = sheet['A1'].value = 'zszxz' print(new_val) # 保存 workbook.save(path)
zszxz666 zszxz
教你使用Python操作excel介绍到这里,更多Python学习请参考编程字典Python教程 和问答部分,谢谢大家对编程字典的支持。
原文链接:https://www.cnblogs.com/wlfya/p/13825404.html