练习:
新建一个sheet
设定一个sheet的插入位置
修改sheet的名称为‘xiaxiaoxu’
设定该sheet的背景标签的颜色
获取全部sheet的名称,打印每个sheet的名称
copy一个sheet
修改copy的sheet的名称为‘xufegnchai‘
保存excel为‘d:\sample.xlsx’
#coding=utf-8
from openpyxl import Workbook
wb=Workbook()
ws=wb.create_sheet('newsheet',0)
print "title before change:", ws.title
ws.title='xiaxiaoxu'
print "title after change:", ws.title
ws.sheet_properties.tabColor='1072BA'
print "*"*50
print "ws.sheet_properties.tabColor: ",ws.sheet_properties.tabColor
print "*"*50
print "wb.sheetnames:",wb.sheetnames
for name in wb.sheetnames:
print 'name%s:'%wb.sheetnames.index(name),name
wb['xiaxiaoxu']['A1']='welcome'
source=wb['xiaxiaoxu']
target=wb.copy_worksheet(source)
print "target.title from copy:",target.title
target.title='xufengchai'
print "target.title after change:",target.title
wb.save('d:\sample.xlsx')
c:Python27Scripts>python task_test.py
title before change: newsheet
title after change: xiaxiaoxu
**************************************************
ws.sheet_properties.tabColor:
<openpyxl.styles.colors.Color object>
Parameters:
tint=0.0, auto=None, theme=None, rgb='001072BA', indexed=None, type='rgb'
**************************************************
wb.sheetnames: [u'xiaxiaoxu', u'Sheet']
name0: xiaxiaoxu
name1: Sheet
target.title from copy: xiaxiaoxu Copy
target.title after change: xufengchai