Python excel exercises: create a new sheet, modify the name, set the color, print the sheet name, copy, save

practise:

Create a new sheet

Set the insertion position of a sheet

Modify the name of the sheet to 'xiaxiaoxu'

Set the color of the background label of the sheet

Get the names of all sheets and print the name of each sheet

copy a sheet

Modify the name of the copied sheet to 'xufegnhai'

Save excel as '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

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324753304&siteId=291194637