python修改sheet名称_openpyxl修改sheet名,sheet颜色,删除sheet的方法

万事万物的操作都是增删改查,sheet也不例外,前面我们讲了openpyxl查询和增加sheet的操作,本节课说下修改和删除的操作。

1、修改sheet名字(通过sheet对象的title属性)

You can change this name at any time with theWorksheet.title property:

ws.title = "New Title"

2、修改sheet名背景颜色(通过sheet对象的sheet_properties.tabColor属性)

The background color of the tab holding this title is white by default. You can change this providing anRRGGBBcolor code to theWorksheet.sheet_properties.tabColor attribute:

ws.sheet_properties.tabColor = "1072BA"

3、删除sheet(通过remove方法)

wb.remove(sheet) #参数为sheet对象而非sheet名

1、修改sheet名代码如下:

# -*- coding: utf-8 -*-

from openpyxl import Workbook

wb = Workbook() # 默认生成一个名为Sheet的sheet

# 创建sheet

for name in ['a','b']:

wb.create_sheet(name)

# 修改sheet名字

num = 1

for sheet in wb:

name = sheet.title + str(num)

sheet.title = name

# 获取sheet名

sheet_names = wb.sheetnames

print(sheet_names)

wb.save('test.xlsx')

D:python3installpython.exe D:/pyscript/py3script/python66/test2/test.py

['Sheet1', 'a1', 'b1']

Process finished with exit code 0

2、修改sheet名文背景色代码如下

# -*- coding: utf-8 -*-

from openpyxl import Workbook

wb = Workbook() # 默认生成一个名为Sheet的sheet

# 创建sheet

for name in ['a','b']:

ws = wb.create_sheet(name)

# 修改sheet名背景色

for sheet in wb:

sheet.sheet_properties.tabColor = '1072BA'

wb.save('test.xlsx')

3、删除sheet

# -*- coding: utf-8 -*-

from openpyxl import Workbook

wb = Workbook() # 默认生成一个名为Sheet的sheet

# 创建sheet

for name in ['a','b']:

ws = wb.create_sheet(name)

# 删除sheet

ws = wb['a']

wb.remove(ws)

wb.save('test.xlsx')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值