目录
一.对sheet的操作
包含对sheet的读取,创建,修改,删除,移动,重命名,以及追加数据
1.1读取sheet
def get_sheet(self,filename):
#获取sheet列表
print(self.workbook.sheetnames)#如['Sheet','Sheet1']
print(self.workbook.worksheets)#如[<Worksheet"sheet">]
print(self.workbook.get_sheet_names())
#获取单个sheet
print('-------------')
print (self.workbook['sheet'])#根据名称
print(self.workbook.sheetnames[0])#根据下标,如sheet
print(self.workbook.worksheets[0])#根据下标,如<worksheet,'sheet'>
1.2创建sheet
def create_sheet(self):
#末尾追加添加
self.workbook.create_sheet('sheet3')
#指定位置添加 (index 从0开始,默认末尾)
self.workbook.create_sheet('sheet4',0)
#若sheet已存在,会默认在名称后面追加数字,如 sheet31,sheet32
self.workbook.create_sheet('sheet3')
#保存
self.workbook.save(self.filename)
1.3复制 sheet
def copy_sheet(self):
#获取sheet对象
sheet=self.workbook['sheet']
#复制sheet(默认名称+copy,例如:sheet copy)
self.workbook.copy_worksheet(sheet)
#保存
self.workbook.save(self.filename)
1.4删除sheet
def delete_sheet(self):
#删除sheet
del self.workbook['sheet copy']
#保存
self.workbook(self.filename)
1.5移动sheet
def move_sheet(self):
#移动sheet(负数:向左移动 正数:向右移动,数值:移动的位置个数)
self.workbook.move_sheet('sheet',-2)
#保存
self.work.save(self.filename)
1.6对sheet重命名
def rename_sheet(self):
#重命名sheet
self.workbook['sheet2'].title='sheet123'
#保存
self.workbook.save(self.filename)
二.对数据的操作
对工作表中数据进行查询数据,删除数据,修改数据,追加数据,遍历数据,读取表头等操作
2.1查询数据
查询sheet中数据,查询单元格中数据
def select_data(self):
#获取sheet
sheet:Worksheet=self.workbook['sheet']
#查询属性
print(f'title:{sheet.title}')#标题
print(f'dimensions:{sheet.dimensions}')#表格大小
print(f'min_row{sheet.min_row}')#最小行
print(f'max_row:{sheet.max_row}')#最大行
print(f'rows:{sheet.rows}')#行对象
print(f'min_column:{sheet.min_column}')#最小列
print(f'max_column:{sheet.max_column}')#最大列
print(f'columns:{sheet.columns}')#列对象
#查询单元格数据
print(sheet['A1'].value)
#保存
self.workbook.save(self.filename)
2.2追加数据
def append_data(self):
#获取sheet对象
sheet:Worksheet =self.workbook['sheet']
#追加数据
sheet.append(['商品','单价','数量'])
sheet.append(['毛巾','12','100'])
sheet.append(['饮料','5','100'])
#保存
self.workbook.save(self.filename)
2.3修改数据
def update_data(self):
#获取sheet对象
sheet: Worksheet=self.workbook['sheet']
#修改数据
sheet['A1']='商品'
sheet.cell(row=2,column=1).value='商品'
sheet.cell(row=3,column=1).value='单价'
#保存
self.workbook.save(self.filename)
2.4删除数据
def delete_data(self):
sheet: Worksheet=self.workbook['sheet']
#删除数据
sheet.delete_row(index=1,amount=1)#第index行开始往下删除amount行
sheet.delete_cols(index=1,amount=5)#第index列开始往后删除amount列
#保存
self.workbook.save(self.filename)
2.5遍历数据
def read_excel(filename):
#遍历sheet中的数据
lwb=openpyxl.load_workbook(filename)
sheet: Worksheet=lwb['sheet']
#按行遍历
for row in sheet.iter_rows():
for cell in row:
print(cell.row,cell.column,cell.value)
print('---------------------------------------')
#按列遍历
for col in sheet.iter_cols():
for cell in col:
print(cell.row,cell.column,cell.value)
2.6读取表头
def get_sheet_head(self):
sheet:Worksheet=self.workbook['sheet']
#获取表头,其中value_only=true表示只获取值
for i in sheet.iter_cols(min_col=1,max_row=1,value_only=True):
print(i)
三.对单元格数据的操作
对单元格中数据进行查询单元格数据,修改单元格数据
3.1查询单元格数据
def get_cell(self):
sheet: Worksheet=self.workbook['sheet']
#获取单个单元格数据
cell1=sheet['A1']
cell2=sheet.cell(1,1)
print(cell1.value)
print(cell2.value)
#查询多个单元格数据
cells=sheet['A1:B2']
for item in cells:
for cell in item:
print(cell.row,cell.column,cell.value)
3.2修改单元格数据
def update_cell(self):
sheet:Worksheet=self.workbook['sheet']
#修改单元格的值
cell1=sheet['A1']
cell1.value='货物'
#保存
self.workbook.save(self.filename)
代码:
# -*— coding:utf-8 -*-
# 姓名 :陶敏
# 创建时间:2024/5/8 11:23
# 文件名: 01demo
import openpyxl
from openpyxl.worksheet.worksheet import Worksheet
class Excel(object):
def __init__(self,filename):
self.workbook=openpyxl.load_workbook(filename)
self.filename=filename
def losd_workbook(self,filename):
#获取工作簿对象,filename:文件的路径
workbook=openpyxl.load_workbook(filename)
#查看工作簿支持的方法和属性
#print(help(workbook))
#常用的属性
print(f'active:{workbook.active}')#当前活动的sheet
print(f'read_only:{workbook.read_only}')#是否以只读方式打开
print(f'encoding:{workbook.encoding}')#编码
print(f'properties:{workbook.properties}')#属性类 如:标签,作者,创作日期等
print(f'title:{workbook.properties}')#标题
print(f'creator:{workbook.properties}')#作者
print(f'created:{workbook.properties}')#创作日期
def get_sheet(self,filename):
#获取sheet列表
print(self.workbook.sheetnames)#如['Sheet','Sheet1']
print(self.workbook.worksheets)#如[<Worksheet"sheet">]
print(self.workbook.get_sheet_names())
#获取单个sheet
print('-------------')
print (self.workbook['sheet'])#根据名称
print(self.workbook.sheetnames[0])#根据下标,如sheet
print(self.workbook.worksheets[0])#根据下标,如<worksheet,'sheet'>
def create_sheet(self):
#末尾追加添加
self.workbook.create_sheet('sheet3')
#指定位置添加 (index 从0开始,默认末尾)
self.workbook.create_sheet('sheet4',0)
#若sheet已存在,会默认在名称后面追加数字,如 sheet31,sheet32
self.workbook.create_sheet('sheet3')
#保存
self.workbook.save(self.filename)
def copy_sheet(self):
#获取sheet对象
sheet=self.workbook['sheet']
#复制sheet(默认名称+copy,例如:sheet copy)
self.workbook.copy_worksheet(sheet)
#保存
self.workbook.save(self.filename)
def delete_sheet(self):
#删除sheet
del self.workbook['sheet copy']
#保存
self.workbook(self.filename)
def move_sheet(self):
#移动sheet(负数:向左移动 正数:向右移动,数值:移动的位置个数)
self.workbook.move_sheet('sheet',-2)
#保存
self.work.save(self.filename)
def rename_sheet(self):
#重命名sheet
self.workbook['sheet2'].title='sheet123'
#保存
self.workbook.save(self.filename)
def append_data(self):
#获取sheet对象
sheet:Worksheet =self.workbook['sheet']
#追加数据
sheet.append(['商品','单价','数量'])
sheet.append(['毛巾','12','100'])
sheet.append(['饮料','5','100'])
#保存
self.workbook.save(self.filename)
def select_data(self):
#获取sheet
sheet:Worksheet=self.workbook['sheet']
#查询属性
print(f'title:{sheet.title}')#标题
print(f'dimensions:{sheet.dimensions}')#表格大小
print(f'min_row{sheet.min_row}')#最小行
print(f'max_row:{sheet.max_row}')#最大行
print(f'rows:{sheet.rows}')#行对象
print(f'min_column:{sheet.min_column}')#最小列
print(f'max_column:{sheet.max_column}')#最大列
print(f'columns:{sheet.columns}')#列对象
#查询单元格数据
print(sheet['A1'].value)
#保存
self.workbook.save(self.filename)
def update_data(self):
#获取sheet对象
sheet: Worksheet=self.workbook['sheet']
#修改数据
sheet['A1']='商品'
sheet.cell(row=2,column=1).value='商品'
sheet.cell(row=3,column=1).value='单价'
#保存
self.workbook.save(self.filename)
def delete_data(self):
sheet: Worksheet=self.workbook['sheet']
#删除数据
sheet.delete_row(index=1,amount=1)#第index行开始往下删除amount行
sheet.delete_cols(index=1,amount=5)#第index列开始往后删除amount列
#保存
self.workbook.save(self.filename)
def get_sheet_head(self):
sheet:Worksheet=self.workbook['sheet']
#获取表头,其中value_only=true表示只获取值
for i in sheet.iter_cols(min_col=1,max_row=1,value_only=True):
print(i)
def read_excel(filename):
#遍历sheet中的数据
lwb=openpyxl.load_workbook(filename)
sheet: Worksheet=lwb['sheet']
#按行遍历
for row in sheet.iter_rows():
for cell in row:
print(cell.row,cell.column,cell.value)
print('---------------------------------------')
#按列遍历
for col in sheet.iter_cols():
for cell in col:
print(cell.row,cell.column,cell.value)
def get_cell(self):
sheet: Worksheet=self.workbook['sheet']
#获取单个单元格数据
cell1=sheet['A1']
cell2=sheet.cell(1,1)
print(cell1.value)
print(cell2.value)
#查询多个单元格数据
cells=sheet['A1:B2']
for item in cells:
for cell in item:
print(cell.row,cell.column,cell.value)
def update_cell(self):
sheet:Worksheet=self.workbook['sheet']
#修改单元格的值
cell1=sheet['A1']
cell1.value='货物'
#保存
self.workbook.save(self.filename)
if __name__=='__main__':
test=Excel('新建 XLSX 工作表.xlsx')
test.get_sheets()
# test.create_sheet()
# test.append_data()
# test.copy_sheet()
# test.delete_data()
# test.delete_sheet()
# test.get_cell()
# test.get_sheet()
# test.get_sheet_head()
# test.move_sheet()
# test.read_excel()
# test.rename_sheet()
# test.select_data()
# test.update_cell()
# test.update_data()