11-Python操作excel
1、python操作excel需要用到的库
python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库。可以直接pip安装这两个库,pip install xlrd 、pip install xlwt
2、python封装常用读取excel方法
# coding:utf-8
import xlrd
import sys # 解决Unicode equal comparison failed to convert both arguments to Unicode - interpreting问题
# uncode编码警告:在unicode等价比较中,把两个参数同时转换为unicode编码失败。中断并认为他们不相等
# windows下的字符串str默认编码是ascii,而python编码是utf8
reload(sys)
sys.setdefaultencoding('utf8') class OperExcel:
def __init__(self, filename=None, sheet_id=None):
if filename:
self.filename = filename
self.sheet_id = sheet_id
else:
self.filename = '../dataconfig/test.xlsx'
self.sheet_id = 0
self.data = self.get_dataBySheetId() # 根据sheetid获取sheet内容
def get_dataBySheetId(self):
data = xlrd.open_workbook(self.filename)
tables = data.sheets()[self.sheet_id]
return tables # 根据sheet名称获取sheet内容
def get_dataBySheetName(self,filepath,sheetname):
data = xlrd.open_workbook(filepath)
tables = data.sheet_by_name(sheetname)
return tables # 获取sheet表行数
def get_lines(self):
tables = self.data
return tables.nrows # 根据行列值获取单元格内容
def get_cell_value(self, row, col):
tables = self.data
return tables.cell_value(row, col) # 根据列头名称,获取所在列Index
def get_columnindex(self, colName):
tables = self.data
columnIndex = None
for i in range(tables.ncols):
if (tables.cell_value(0, i) == colName):
columnIndex = i
break
return columnIndex # 根据列Index和单元格值获取所在行
def get_rowIndex_ByColIndexAndValue(self,colIndex,cellValue):
tables = self.data
rowIndex = None
for i in range (tables.nrows):
if (tables.cell_value(i,colIndex) == cellValue):
roeIndex = i
break;
return i if __name__ == '__main__':
oper = OperExcel()
print oper.get_lines()
print oper.get_cell_value(1, 1)
print oper.get_columnindex('密码')
print oper.get_rowIndex_ByColIndexAndValue(1,'ww123456')
print oper.get_dataBySheetName('../dataconfig/test.xlsx','Sheet1').cell_value(1,1)
11-Python操作excel的更多相关文章
- python操作excel表格(xlrd/xlwt)
最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...
- Python 利用Python操作excel表格之openyxl介绍Part2
利用Python操作excel表格之openyxl介绍 by:授客 QQ:1033553122 欢迎加入全国软件测试交流qq群(群号:7156436) ## 绘图 c = LineChart() ...
- 【转】python操作excel表格(xlrd/xlwt)
[转]python操作excel表格(xlrd/xlwt) 最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异, ...
- python操作excel xlrd和xlwt的使用
最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...
- python 操作excel 的包 函数
###########sample 1 https://blog.csdn.net/chengxuyuanyonghu/article/details/54951399 python操作excel主要 ...
- 运营的Python指南 - Python 操作Excel
这是一份写给运营人员的Python指南.本文主要讲述如何使用Python操作Excel.完成Excel的创建,查询和修改操作. 相关代码请参考 https://github.com/RustFishe ...
- python - 操作excel表格
说明:由于公司oa暂缺,人事妹子在做考勤的时候,需要通过几个excel表格去交叉比对员工是否有旷工或迟到,工作量大而且容易出错. 这时候it屌丝的机会来啦,花了一天时间给妹子撸了一个自动化脚本. 1. ...
- 用Python操作excel文档
使用Python第三方库 这一节我们学习如何使用Python去操作Excel文档.如果大家有人不知道Excel的话,那么建议先学一学office办公基础.这里想要操作Excel,必须安装一个Pytho ...
- 转载:python操作excel表格(xlrd/xlwt)
python操作excel表格(xlrd/xlwt) 最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而 ...
- Python操作Excel
一.系统性学习 对于操作Excel,需要Xlrd/xlwt这两个模块,下面推荐出系统性学习的网址: python操作Excel读写--使用xlrd 官方文档 Python 使用 Xlrd/xlwt 操 ...
随机推荐
- json和jquery中的ajax
JSON: java script Object otation:js对象标记 声明一个json对象,使用key:value对应,中间用冒号连接,键值对之间用逗号连接,最外面用{}包含 声明方式: 语 ...
- 使用vue-cli@3启动elementui脚手架
[vue3.x] 准备看elementui的源码,早上拉elementui提供的脚手架代码,于是下载了vue3.x(之前一直用2.x) 1.先把vue2.x卸载了 npm uninstall -g v ...
- Codeforces Round #552 (Div. 3) B题
题目链接:http://codeforces.com/contest/1154/problem/B 题目大意:给出n个数,每个数都可以加上或减去这个一个数D,求对这n个数操作之后当所有数都相等时,D的 ...
- pythone函数基础(12)连接Redis,写数据,读数据,修改数据
需要导入Resdis模块 import redisip = '127.0.0.1'password='123456'r = redis.Redis(host=ip,password=password, ...
- 控制台 console.write 中文为问号
原因: 因为你当前环境代码页是437,是美国英语的字符编码 解决方式: 你把你环境设置成936就是简体中文字符编码环境了 Console.OutputEncoding = Encoding.GetEn ...
- webpack4.0
1. webpack 刚开始是js的模块打包,现在是一个任何模块打包工具 可以识别 CommonJS引入规范 CMD AMD 2. commonJS: module.exports r ...
- 苹果手机input有圆角阴影的解决方法
input[type=button], input[type=submit], input[type=file], button { cursor: pointer; -webkit-appearan ...
- git hub命令,上传到github
git hub命令,上传到github 1,git init; 初始化 2,git config --global user.email " ....@.... ...
- Java多线程中static变量的使用
轉:https://blog.csdn.net/yy304935305/article/details/52456771 鲁迅先生曾说过:“时间就像海绵里的水,只要愿挤,总还是有的”.不管肿(怎)么说 ...
- php的运行原理、cgi对比fastcgi以及php-cgi和php-fpm之间的联系区别
最近项目中本地测试环境遇到了windows环境下的nginx使用file_get_contents/curl访问php文件导致的阻塞问题,一直在找解决的方案,这个问题研究了三天终于找到了解决方案,特别 ...