##操作单列
#操作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>)

python excel操作 练习-#操作单列 #操作A到C列 #操作1到3行 #指定一个范围遍历所有行和列 #获取所有行 #获取所有列的更多相关文章

  1. dplyr 数据操作 列操作(select / mutate)

    在R中,我们通常需要对数据列进行各种各样的操作,比如选取某一列.重命名某一列等. dplyr中的select函数子在数据列的操作上也同样表现了它的简洁性,而且各种操作眼花缭乱. select(.dat ...

  2. python excel操作总结

    1.openpyxl包的导入 Dos命令行输入 pip install openpyxl==2.3.3 这里注意一下openpyxl包的版本问题 版本装的太高有很多api不支持了,所以笔者这里用的是2 ...

  3. Python Excel操作——xlrd、xlwd

    读取 1.导入模块 import xlrd 2.打开Excel文件读取数据 data = xlrd.open_workbook('excel.xls') 3.获取一个工作表 1 table = dat ...

  4. python中openpyxl的用法【安装,以及一些基本的操作】

    概述 Openpyxl是python中简单易用的操作excel电子表格的一个模块.接下来呢,跟博主一起学习一下吧  ----_<_>_---- 首先先清楚一些excel的基本概念: 在op ...

  5. 小白学 Python 数据分析(6):Pandas (五)基础操作(2)数据选择

    人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 小白学 Python 数据分析(2):Pandas (一)概述 小白学 Python 数据分析(3):P ...

  6. Python教程:连接数据库,对数据进行增删改查操作

    各位志同道合的同仁可以点击上方关注↑↑↑↑↑↑ 本教程致力于程序员快速掌握Python语言编程. 本文章内容是基于上次课程Python教程:操作数据库,MySql的安装详解 和python基础知识之上 ...

  7. python/Django(增、删、改、查)操作

    python/Django(增.删.改.查)操作 我们要通过pycharm中的Django模块连接MySQL数据库进行对数据的操作. 一.创建Django项目(每创建一个项目都要进行以下设置) 1.如 ...

  8. python之文件的读写和文件目录以及文件夹的操作实现代码

    这篇文章主要介绍了python之文件的读写和文件目录以及文件夹的操作实现代码,需要的朋友可以参考下 为了安全起见,最好还是给打开的文件对象指定一个名字,这样在完成操作之后可以迅速关闭文件,防止一些无用 ...

  9. python字典dict的增、删、改、查操作

    ## python字典dict的增.删.改.查操作dict = {'age': 18, 'name': 'jin', 'sex': 'male', }#增# dict['heigh'] = 185 # ...

随机推荐

  1. webconfig的配置说明

    转自 :http://www.cnblogs.com/kissdodog/archive/2013/04/16/3025315.html <?xml version="1.0" ...

  2. mysql的启动脚本mysql.server及示例配置文件

    以MySQL-server-4.0.14-0.i3862881064151.rpm为例,放在/data目录下 cd /data rpm -ivh MySQL-server-4.0.14-0.i386. ...

  3. Java-06-动手动脑

    1.为什么子类的构造方法在运行之前,必须调用父类的构造方法?能不能反过来?为什么不能反过来? 因为子类继承于父类,子类中有父类的对象,父类的构造方法初始化后,子类才能运行自己的构造方法 不能放过来,继 ...

  4. easyui datagrid 加载 历险记(dom中已经加载,fit:true 时改变浏览器大小是会显示出来)

    (dom中已经加载,fit:true 时改变浏览器大小是会显示出来) 第一个想到的就是resize datagird  大小!可是没有用 ... 答案是加载的的div height为0导至的(//To ...

  5. HDU-1011 Starship Troopers(树形dp)

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

  6. Sublime2或3配置R、Scala、Python交互式环境

    1.Sublime3的下载地址:http://www.sublimetext.com/3 2.刚刚安装的软件是没有PackageControl的,需要在新安装使用 (1)   以前没有安装过Packa ...

  7. codeforces 592B/C

    题目链接:http://codeforces.com/contest/592/problem/B B. The Monster and the Squirrel time limit per test ...

  8. 启用mapredure历史服务器方法

    在mapred-site.xml配置文件中添加如下信息: <property>                <name>mapreduce.jobhistory.addres ...

  9. [Error] 'for' loop initial declarations are only allowed in C99 or C11 mode

    #include <stdio.h> #include <stdlib.h> #define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量 #defin ...

  10. jdb--gdb---java 远程调试(java application与web application)

    命令比较     gdb              jdb     bt             where     del           clear     stop         brea ...