1、创建excel、excel单元格操作(读、写)、删除excel工作薄
import openpyxl
'''
openpyxl读取excel工作表
'''
if __name__ == '__main__':
'''load_workbook()获取工作簿对象'''
file_1 = openpyxl.load_workbook('捉虫行动统计.xlsx') # 这里是相对路径
'''get_sheet_names()获取对应工作簿中所有表名的列表'''
print(file_1.get_sheet_names()) # ['Sheet1', 'issues', '其他']
'''get_sheet_by_name()从工作薄中取得工作表对象'''
sheet_1 = file_1.get_sheet_by_name(file_1.get_sheet_names()[1])
# sheet_1 = file_1.get_sheet_by_name('issues')
print(sheet_1, type(sheet_1)) # <Worksheet "issues"> <class 'openpyxl.worksheet.worksheet.Worksheet'>
'''获取工作表内容'''
print(sheet_1['A1']) # <Cell 'issues'.A1>
print(sheet_1['A1'].value) # test
print(sheet_1['A2'].value) # 测试
print(sheet_1['B3'].value) # 不存在返回None
'''使用cell方法获取工作表内容'''
a = sheet_1.cell(row=1, column=1)
print(a) # <Cell 'issues'.A1>
print(a.value) # test
"遍历获取excel中某列的内容"
for i in range(2, 10): # excel中行从1开始
print(sheet_1.cell(row=i, column=5).value) # 获取E1 E2 E3位置的内容
"获取表的大小"
max_row = sheet_1.max_row
max_column = sheet_1.max_column
print(max_row, max_column) # 611 20
"遍历获取excel中某列的内容"
result = []
for i in range(2, max_row+1): # excel中行从1开始
author = sheet_1.cell(row=i, column=5).value.strip('\t').strip() # 获取E列的内容
result.append(author)
print(result)
"sheet_1.columns获取指定列"
print(sheet_1.columns) # <generator object Worksheet._cells_by_col at 0x000001C198355C10>
list(sheet_1.columns) # 返回所有的列转换为列表,每一列为一个元组,作为一个列表元素
print(list(sheet_1.columns)[4]) # 通过索引获取第5列的所有工作表对象
for col in list(sheet_1.columns)[4]:
print(col.value) # 获取第五列内容
"sheet_1.rows获取指定行"
print(sheet_1.rows) # <generator object Worksheet._cells_by_row at 0x00000202881F1F90>
list(sheet_1.rows) # 返回所有的行转换为列表,每一行为一个元组,作为一个列表元素
print(list(sheet_1.rows)[4]) # 通过索引获取第5行的所有工作表对象
for col in list(sheet_1.rows)[4]:
print(col.value) # 获取第五行内容
"列字母与数字之间的转换"
a = openpyxl.utils.get_column_letter(100) # 将数字转换为字母 CV
b = openpyxl.utils.column_index_from_string('CV') #将字母转换为数字 100
print(a, b)
"对表格切片"
print(sheet_1['M2':'N3']) # 返回元组,子元组为一行
'''
((<Cell 'Issue'.M2>, <Cell 'Issue'.N2>), (<Cell 'Issue'.M3>, <Cell 'Issue'.N3>))
'''
"嵌套循环读取切片内容"
for line in sheet_1['M2':'N3']: # line 是子元组为一行,(<Cell 'Issue'.M2>, <Cell 'Issue'.N2>)
for one_cell in line:
print(one_cell.value) # 将每行的内容打印出来
'openpyxl.Workbook()创建excel文件'
ex_file = openpyxl.Workbook()
print(ex_file) # <openpyxl.workbook.workbook.Workbook object at 0x00000201116C5190>
sheets = ex_file.get_sheet_names() # 默认工作表为Sheet
print(sheets) # ['Sheet']
'''sheet_1.title修改工作表名称'''
sheet_1 = ex_file.get_sheet_by_name(sheets[0])
# sheet_1 = ex_file.get_active_sheet()
print(sheet_1)
sheet_1.title = '新数据表'
print(ex_file.get_sheet_names())
'''在cell中输入内容,与字典存入数据相似'''
sheet_1['A2'] = '测试内容'
print(sheet_1['A2']) # <Cell '新数据表'.A2>
print(sheet_1['A2'].value) # 测试内容
'''在区域内一次写入内容'''
for line in sheet_1['B1':'C4']:
for cell in line: # cell 为单元格对象 <Cell '新数据表'.B1>
sheet_1[cell.coordinate] = '测试内容' # cell.coordinate属性获取单元格的行列信息
# print(cell.coordinate, cell.row, cell.column) # B1 1 2
'''
cell.coordinate属性获取单元格的行列信息 返回B1
cell.row属性获取单元格的行信息 返回1
cell.column属性获取单元格的列信息 返回2
'''
'''ex_file.create_sheet()增加工作表'''
ex_file.create_sheet('新数据表2')
ex_file.create_sheet('新数据表4', index=0) # 制定创建工作表的位置 index从0开始
print(ex_file.get_sheet_names()) # ['新数据表', '新数据表2']
'''ex_file.remove_sheet()删除工作表'''
# ex_file.remove_sheet('新数据表2') # ValueError: '新数据表2' is not in list
print(ex_file.get_sheet_by_name('新数据表2')) # <Worksheet "新数据表2">
ex_file.remove_sheet(ex_file.get_sheet_by_name('新数据表2'))
"ex_file.save()保存文件到本地"
ex_file.save("excel创建文件.xlsx")