⑨ .copy_worksheet():复制一个sheet表到另外一张excel表
这个操作的实质,就是复制某个excel表中的sheet表,然后将文件存储到另外一张excel表中;
workbook = load_workbook(filename = "a.xlsx") sheet = workbook.active print("a.xlsx中有这几个sheet表",workbook.sheetnames) sheet = workbook['姓名'] workbook.copy_worksheet(sheet) workbook.save(filename = "test.xlsx")
结果如下:
⑩ sheet.title:修改sheet表的名称
.title = “新的sheet表名”
workbook = load_workbook(filename = "a.xlsx") sheet = workbook.active print(sheet) sheet.title = "我是修改后的sheet名" print(sheet)
结果如下:
⑪ 创建新的excel表格文件
from openpyxl import Workbook workbook = Workbook() sheet = workbook.active sheet.title = "表格1" workbook.save(filename = "新建的excel表格")
结果如下:
⑫ sheet.freeze_panes:冻结窗口
.freeze_panes = “单元格”
workbook = load_workbook(filename = "花园.xlsx") sheet = workbook.active print(sheet) sheet.freeze_panes = "C3" workbook.save(filename = "花园.xlsx") """ 冻结窗口以后,你可以打开源文件,进行检验; """
结果如下:
⑬ sheet.auto_filter.ref:给表格添加“筛选器”
.auto_filter.ref = sheet.dimension 给所有字段添加筛选器;
.auto_filter.ref = “A1” 给A1这个格子添加“筛选器”,就是给第一列添加“筛选器”;
workbook = load_workbook(filename = "花园.xlsx") sheet = workbook.active print(sheet) sheet.auto_filter.ref = sheet["A1"] workbook.save(filename = "花园.xlsx")
结果如下:
4、批量调整字体和样式
1)修改字体样式
Font(name=字体名称,size=字体大小,bold=是否加粗,italic=是否斜体,color=字体颜色)
from openpyxl.styles import Font from openpyxl import load_workbook workbook = load_workbook(filename="花园.xlsx") sheet = workbook.active cell = sheet["A1"] font = Font(name="微软雅黑",size=20,bold=True,italic=True,color="FF0000") cell.font = font workbook.save(filename = "花园.xlsx") """ 这个color是RGB的16进制表示,自己下去百度学习; """
结果如下: