python操作excel之xlrd
2015-06-10 09:16
519 查看
xlrd是专门用来在python中读取微软execel的模块,可以自己直接下载安装,也可以通过包管理器安装。组件的官网地址:http://www.python-excel.org/
基本操作:
#-*-encoding:utf-8-*- #设置编码方式
import xlrd #导入xlrd模块
#打开指定文件路径的excel文件
xlsfile = r'D:\AutoPlan\apisnew.xls'
book = xlrd.open_workbook(xlsfile) #获得excel的book对象
#获取sheet对象,方法有2种:
sheet_name=book.sheet_names()[0] #获得指定索引的sheet名字
print sheet_name
sheet1=book.sheet_by_name(sheet_name) #通过sheet名字来获取,当然如果你知道sheet名字了可以直接指定
sheet0=book.sheet_by_index(0) #通过sheet索引获得sheet对象
#获取行数和列数:
nrows = sheet.nrows #行总数
ncols = sheet.ncols #列总数
#获得指定行、列的值,返回对象为一个值列表
row_data = sheet.row_values(0) #获得第1行的数据列表
col_data = sheet.col_values(0) #获得第一列的数据列表,然后就可以迭代里面的数据了
#通过cell的位置坐标获得指定cell的值
cell_value1 = sheet.cell_value(0,1) ##只有cell的值内容,如http://xxx.xxx.xxx.xxx:8850/2/photos/square/
print cell_value1
cell_value2 = sheet.cell(0,1) ##除了cell值内容外还有附加属性,如:text:u'http://xxx.xxx.xxx.xxx:8850/2/photos/square/'
print cell_value2
是不是很方便啊,恩,比用vbs调用的excel COM对象简便多了。而且这个支持linux平台。
========xls的写方法使用xlwt块==========
#encoding:utf-8 #设置编码方式
import xlwt
wbk = xlwt.Workbook(encoding='utf-8', style_compression=0)
heet = wbk.add_sheet('sheet 1', cell_overwrite_ok=True) ##第二参数用于确认同一个cell单元是否可以重设值。
sheet.write(0,0,'some text')
sheet.write(0,0,'this should overwrite') ##重新设置,需cell_overwrite_ok=True
style = xlwt.XFStyle()
font = xlwt.Font()
font.name = 'Times New Roman'
font.bold = True
style.font = font
sheet.write(0, 1, 'some bold Times text', style)
wbk.save('D:\TestData2.xls') ##该文件名必须存在
基本操作:
#-*-encoding:utf-8-*- #设置编码方式
import xlrd #导入xlrd模块
#打开指定文件路径的excel文件
xlsfile = r'D:\AutoPlan\apisnew.xls'
book = xlrd.open_workbook(xlsfile) #获得excel的book对象
#获取sheet对象,方法有2种:
sheet_name=book.sheet_names()[0] #获得指定索引的sheet名字
print sheet_name
sheet1=book.sheet_by_name(sheet_name) #通过sheet名字来获取,当然如果你知道sheet名字了可以直接指定
sheet0=book.sheet_by_index(0) #通过sheet索引获得sheet对象
#获取行数和列数:
nrows = sheet.nrows #行总数
ncols = sheet.ncols #列总数
#获得指定行、列的值,返回对象为一个值列表
row_data = sheet.row_values(0) #获得第1行的数据列表
col_data = sheet.col_values(0) #获得第一列的数据列表,然后就可以迭代里面的数据了
#通过cell的位置坐标获得指定cell的值
cell_value1 = sheet.cell_value(0,1) ##只有cell的值内容,如http://xxx.xxx.xxx.xxx:8850/2/photos/square/
print cell_value1
cell_value2 = sheet.cell(0,1) ##除了cell值内容外还有附加属性,如:text:u'http://xxx.xxx.xxx.xxx:8850/2/photos/square/'
print cell_value2
是不是很方便啊,恩,比用vbs调用的excel COM对象简便多了。而且这个支持linux平台。
========xls的写方法使用xlwt块==========
#encoding:utf-8 #设置编码方式
import xlwt
wbk = xlwt.Workbook(encoding='utf-8', style_compression=0)
heet = wbk.add_sheet('sheet 1', cell_overwrite_ok=True) ##第二参数用于确认同一个cell单元是否可以重设值。
sheet.write(0,0,'some text')
sheet.write(0,0,'this should overwrite') ##重新设置,需cell_overwrite_ok=True
style = xlwt.XFStyle()
font = xlwt.Font()
font.name = 'Times New Roman'
font.bold = True
style.font = font
sheet.write(0, 1, 'some bold Times text', style)
wbk.save('D:\TestData2.xls') ##该文件名必须存在
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- 微软无线镭射简报鲨8000激光笔记本鼠标 - (2)
- 对《大家都在点赞 Windows Terminal,我决定给你泼一盆冷水》一文的商榷
- Excel 曝出 Power Query 安全漏洞,1.2 亿用户易受远程 DDE 攻击
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定