zoukankan      html  css  js  c++  java
  • python excel操作 练习-#操作单列 #操作A到C列 #操作1到3行 #指定一个范围遍历所有行和列 #获取所有行 #获取所有列

    ##操作单列
    #操作A到C列
    #操作1到3行
    #指定一个范围遍历所有行和列
    #获取所有行
    #获取所有列

    #coding=utf-8

    from openpyxl import Workbook

    wb=Workbook()

    ws1=wb.active

    ws1["A1"]=1

    ws1["A2"]=2

    ws1["A3"]=3

    ws1["B1"]=4

    ws1["B2"]=5

    ws1["B3"]=6

    ws1["C1"]=7

    ws1["C2"]=8

    ws1["C3"]=9

    print "*"*50

    #操作单列

    print "ws1['A']"

    print ws1['A']

    for cell in ws1['A']:

        print cell.value

    #操作A到C列

    print "ws1['A:C']"

    print ws1['A:C']

    for column in ws1['A:C']:

        for cell in column:

            print cell.value

    print "*"*50

    #操作1到3行

    print "row_range=ws1[1:3]:"

    row_range=ws1[1:3]

    print row_range

    for row in row_range:

        for cell in row:

            print cell.value

    print "*"*50

    #指定一个范围遍历所有行和列

    print "ws1.iter_rows(min_row=1,max_row=3,min_col=1,max_col=3):"

    for row in ws1.iter_rows(min_row=1,max_row=3,min_col=1,max_col=3):

        for cell in row:

            print cell.value

    print "*"*50

    #获取所有行

    print "ws1.rows:"

    print ws1.rows

    for row in ws1.rows:# ws1.iter_rows()也可以

        print row

    print "*"*50

    #获取所有列

    print "ws1.columns:"

    print ws1.columns

    for col in ws1.columns:# ws1.iter_cols()也可以

        print col

    wb.save('d:\sample.xlsx')

    结果:

    c:Python27Scripts>python task_test.py

    **************************************************

    ws1['A']

    (<Cell u'Sheet'.A1>, <Cell u'Sheet'.A2>, <Cell u'Sheet'.A3>)

    1

    2

    3

    ws1['A:C']

    ((<Cell u'Sheet'.A1>, <Cell u'Sheet'.A2>, <Cell u'Sheet'.A3>), (<Cell u'Sheet'.B1>, <Cell u'Sheet'.B2>, <Cell u'Sheet'.B3>), (<Cell u'Sheet'.C1>, <Cell u'Sheet'.C2>, <Cell u'Sheet'.C3>))

    1

    2

    3

    4

    5

    6

    7

    8

    9

    **************************************************

    row_range=ws1[1:3]:

    ((<Cell u'Sheet'.A1>, <Cell u'Sheet'.B1>, <Cell u'Sheet'.C1>), (<Cell u'Sheet'.A2>, <Cell u'Sheet'.B2>, <Cell u'Sheet'.C2>), (<Cell u'Sheet'.A3>, <Cell u'Sheet'.B3>, <Cell u'Sheet'.C3>))

    1

    4

    7

    2

    5

    8

    3

    6

    9

    **************************************************

    ws1.iter_rows(min_row=1,max_row=3,min_col=1,max_col=3):

    1

    4

    7

    2

    5

    8

    3

    6

    9

    **************************************************

    ws1.rows:

    <generator object _cells_by_row at 0x034C7418>

    (<Cell u'Sheet'.A1>, <Cell u'Sheet'.B1>, <Cell u'Sheet'.C1>)

    (<Cell u'Sheet'.A2>, <Cell u'Sheet'.B2>, <Cell u'Sheet'.C2>)

    (<Cell u'Sheet'.A3>, <Cell u'Sheet'.B3>, <Cell u'Sheet'.C3>)

    **************************************************

    ws1.columns:

    <generator object _cells_by_col at 0x034C7418>

    (<Cell u'Sheet'.A1>, <Cell u'Sheet'.A2>, <Cell u'Sheet'.A3>)

    (<Cell u'Sheet'.B1>, <Cell u'Sheet'.B2>, <Cell u'Sheet'.B3>)

    (<Cell u'Sheet'.C1>, <Cell u'Sheet'.C2>, <Cell u'Sheet'.C3>)

  • 相关阅读:
    C++ 类初始化的顺序
    Given constant integers x and t, write a function that takes no argument and returns true if the function has been called x number of times in last t secs.
    Find out all the elements in A and B such that the A[i]B[j]=C[k]
    顺时针打印数组,美丽版
    Given a string S, find the longest palindromic substring in S.
    很多机器,每台机器上都有很一部分数据,如何输出分布在所有机器上的所有数据的median
    C++'s mutable and conceptual constness
    上20阶楼梯,可以一次迈1,2,3步,请问有多少种上法?
    Count smaller elements on right side in an array.
    一排房子,连续填色,成本最低的问题
  • 原文地址:https://www.cnblogs.com/xiaxiaoxu/p/8927442.html
Copyright © 2011-2022 走看看