import openpyxl
# 读取Excel表格中的数据
def selectExcel():
wb = openpyxl.load_workbook('example.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')
print(tuple(sheet['A1':'C3']))
for rowOfCellObjests in sheet['A1':'C3']:
for cellObj in rowOfCellObjests:
print(cellObj.coordinate,cellObj.value)
print('---Over---')
# 写入Excel文档,修改sheet,并另存为
def updateExcel():
wb = openpyxl.load_workbook('example.xlsx')
sheet = wb.get_active_sheet()
sheet.title = 'test'
wb.save('example_copy.xlsx')
# 创建和删除工作表
def create_remove_sheet():
wb = openpyxl.Workbook()
print('获取当前sheet名称:')
for name in wb.get_sheet_names():
print(name)
wb.create_sheet()
print('新增后,获取当前sheet名称:')
for name in wb.get_sheet_names():
print(name)
wb.create_sheet(index=0,title='First Sheet')
print('指定位置新增后,获取当前sheet名称:')
for name in wb.get_sheet_names():
print(name)
wb.remove_sheet(wb.get_sheet_by_name('Sheet1'))
print('指定名称删除后,获取当前sheet名称:')
for name in wb.get_sheet_names():
print(name)
# 将值写入单元格
def insertExcel():
wb = openpyxl.Workbook()
sheet = wb.get_sheet_by_name('Sheet')
sheet['A1'] = 'hello world!'
print(sheet['A1'].value)
#selectExcel()
#updateExcel()
#create_remove_sheet()
#insertExcel()