创建
from openpyxl import Workbook
wb=Workbook() #创建一个工作表
ws=wb.active #ws操作sheet页
ws1=wb.create_sheet('Mysheetfirst',0)
ws1['A1'] = 'a1'
wb.save('D:\\test2.xlsxx')
操作已存在excel
import openpyxl
from openpyxl import load_workbook
filename = 'D:\\test2.xlsx'
# 打开已有的,没有会报错
wb = load_workbook(filename)
# 创建表
# 插入到最后
ws1 = wb.create_sheet("test")
# 插入到最开始的位置
# ws2 = wb.create_sheet("test", 0)
# wb.save(filename)
# 选择表
ws3 = wb["test"]
# or ws4 = wb.get_sheet_by_name("New Title")
# 显示所有表名
print(wb.sheetnames)
ws3['A1'] = 'a1'
ws3['A2'] = 'a2'
# 保存
wb.save(filename)