一、workbook
创建workbook
from openpyxl import Workbook
wb = Workbook() #得到一个全新的workbook
二、worksheet
1.创建workbook的时候会默认创建出一个worksheet
使用active就可以得到这个worksheet
ws = wb.active #得到一个默认的sheet
2.
如果
你还想要更多的sheet的话可以自己创建一个sheet,方式如下:
ws1 = wb.create_sheet('sheet2') # create a sheet in the last
we2 = wb.create_sheet('sheet3', 0) # create a sheet in the first one
3.通过sheet名字获取到sheet
ws3 = wb['sheet3']
4.给指定的worksheet赋予标题
ws.title = 'new title' # give the sheet a name or title
5.给sheet标题/title 一个背景颜色
ws.sheet_properties.tabColor = 'e7e7e7' # give this title a background color
6.获取workbook中所有sheet标题列表
1.通过一个方法
wb.sheetnames
2.通过一个循环
for sheet in wb:
print(sheet.title)
7.复制整个worksheet
target = wb.copy_worksheet(source) //由此得到一个复制过来的sheet
所有内容均为博主在openpyxl 官网学习的笔记,有兴趣的可以去看看openpyxl官网地址