python excel练习:新建sheet、修改名称、设定颜色、打印sheet名称,复制,保存

python

练习:

新建一个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:\n",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:\Python27\Scripts>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

以上是 python excel练习:新建sheet、修改名称、设定颜色、打印sheet名称,复制,保存 的全部内容, 来源链接: utcz.com/z/388698.html

回到顶部