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


    练习:

    新建一个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

  • 相关阅读:
    Find the Smallest K Elements in an Array
    Count of Smaller Number
    Number of Inversion Couple
    Delete False Elements
    Sort Array
    Tree Diameter
    Segment Tree Implementation
    Java Programming Mock Tests
    zz Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)
    Algorithm about SubArrays & SubStrings
  • 原文地址:https://www.cnblogs.com/xiaxiaoxu/p/8926384.html
Copyright © 2020-2023  润新知