1 概述
1.1 图示
1.2 安装第三方库
2 工作簿 workbook
2.1 创建:Workbook()
import openpyxl
class Excel(object):
def __init__(self):
pass
def create_workbook(self, filename):
"""
创建工作簿对象:workbook
:param filename: 文件名(相对路径 或 绝对路径)
:return:
"""
workbook = openpyxl.Workbook()
workbook.save(filename)
if __name__ == '__main__':
test = Excel()
test.create_workbook('1.xlsx')
2.2 常用属性:load_workbook()
import openpyxl
class Excel(object):
def __init__(self):
pass
def load_workbook(self, filename):
workbook = openpyxl.load_workbook(filename)
print(f'active: {workbook.active}')
print(f'read_only: {workbook.read_only}')
print(f'encoding: {workbook.encoding}')
print(f'properties: {workbook.properties}')
print(f'title: {workbook.properties.title}')
print(f'creator: {workbook.properties.creator}')
print(f'created: {workbook.properties.created}')
if __name__ == '__main__':
test = Excel()
test.load_workbook('1.xlsx')
2.3 获取 sheet
import openpyxl
class Excel(object):
def __init__(self, filename):
self.workbook = openpyxl.load_workbook(filename)
def get_sheets(self):
print(self.workbook.sheetnames)
print(self.workbook.worksheets)
print('------------')
print(self.workbook['Sheet'])
print(self.workbook.sheetnames[0])
print(self.workbook.worksheets[0])
if __name__ == '__main__':
test = Excel('1.xlsx')
test.get_sheets()
2.4 创建 sheet
import openpyxl
class Excel(object):
def __init__(self, filename):
self.workbook = openpyxl.load_workbook(filename)
self.filename = filename
def create_sheet(self):
self.workbook.create_sheet('Sheet3')
self.workbook.create_sheet('Sheet4', 0)
self.workbook.create_sheet('Sheet3')
self.workbook.save(self.filename)
if __name__ == '__main__':
test = Excel('1.xlsx')
test.create_sheet()
2.5 复制 sheet
import openpyxl
class Excel(object):
def __init__(self, filename):
self.workbook = openpyxl.load_workbook(filename)
self.filename = filename
def copy_sheet(self):
sheet = self.workbook['Sheet']
self.workbook.copy_worksheet(sheet)
self.workbook.save(self.filename)
if __name__ == '__main__':
test = Excel('1.xlsx')
test.copy_sheet()
2.6 删除 sheet
import openpyxl
class Excel(object):
def __init__(self, filename):
self.workbook = openpyxl.load_workbook(filename)
self.filename = filename
def delete_sheet(self):
del self.workbook['Sheet Copy']
self.workbook.save(self.filename)
if __name__ == '__main__':
test = Excel('1.xlsx')
test.delete_sheet()
2.7 移动 sheet
import openpyxl
class Excel(object):
def __init__(self, filename):
self.workbook = openpyxl.load_workbook(filename)
self.filename = filename
def move_sheet(self):
self.workbook.move_sheet('Sheet', -1)
self.workbook.save(self.filename)
if __name__ == '__main__':
test = Excel('1.xlsx')
test.move_sheet()
2.8 重命名 Sheet
import openpyxl
class Excel(object):
def __init__(self, filename):
self.workbook = openpyxl.load_workbook(filename)
self.filename = filename
def rename_sheet(self):
self.workbook['Sheet1'].title = 'Sheet111'
self.workbook.save(self.filename)
if __name__ == '__main__':
test = Excel('1.xlsx')
test.rename_sheet()
3 工作表 sheet
3.1 追加数据
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 append_data(self):
sheet: Worksheet = self.workbook['Sheet']
sheet.append(['姓名', '性别', '年龄'])
sheet.append(['张三', '女', 18])
sheet.append(['李四', '男', 19])
sheet.append(['王五', '女', 20])
self.workbook.save(self.filename)
if __name__ == '__main__':
test = Excel('1.xlsx')
test.append_data()
3.2 查询数据
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 select_data(self):
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)
if __name__ == '__main__':
test = Excel('1.xlsx')
test.select_data()
3.3 修改数据
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 update_data(self):
sheet: Worksheet = self.workbook['Sheet']
sheet['A1'] = '我是A1'
sheet.cell(row=2, column=1).value = '我是A2'
sheet.cell(row=3, column=1, value='我是A3')
self.workbook.save(self.filename)
if __name__ == '__main__':
test = Excel('1.xlsx')
test.update_data()
3.4 删除数据
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 update_data(self):
sheet: Worksheet = self.workbook['Sheet']
sheet.delete_rows(idx=1, amount=1)
sheet.delete_cols(idx=2, amount=2)
self.workbook.save(self.filename)
if __name__ == '__main__':
test = Excel('1.xlsx')
test.update_data()
3.5 获取表头
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 get_sheet_head(self):
sheet: Worksheet = self.workbook['Sheet']
for i in sheet.iter_cols(min_col=1, max_row=1, values_only=True):
print(i)
if __name__ == '__main__':
test = Excel('1.xlsx')
test.get_sheet_head()
3.6 遍历数据
import openpyxl
from openpyxl.worksheet.worksheet import Worksheet
def read_excel(filename):
"""
遍历 sheet 中的数据
:param filename: 文件名
"""
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('------------------ 分割线1 -----------------')
for col in sheet.iter_cols():
for cell in col:
print(cell.row, cell.column, cell.value)
if __name__ == '__main__':
fileName = '1.xlsx'
read_excel(fileName)
4 单元格 cell
4.1 查询
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 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)
if __name__ == '__main__':
test = Excel('1.xlsx')
test.get_cell()
4.2 修改
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 update_cell(self):
sheet: Worksheet = self.workbook['Sheet']
cell1 = sheet['A1']
cell1.value = 'A1'
self.workbook.save(self.filename)
if __name__ == '__main__':
test = Excel('1.xlsx')
test.update_cell()