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

本文详细介绍使用 openpyxl 库操作 Excel 表格的方法,包括如何读写单元格数据、操作单列或多列、操作单行或多行等。通过具体实例展示了如何遍历表格中的数据,并提供了获取所有行和所有列的操作技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

##操作单列
#操作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:\Python27\Scripts>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>)

 

转载于:https://www.cnblogs.com/xiaxiaoxu/p/8927442.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值