Python处理Excel常用操作就是读和写,我的需求是需要在原excel文件中进行读写操作。共用到了两个模块xlrd和openpyxl,这两个模块都是需要自己去安装的。openpyxl只能用来处理 Excel 2007 及以上版本的 excel 文件,也就是 .xlsx/.xlsm 格式的表格文件,它对文件大小没有限制;其中xlwt针对Ecxec2007之前的版本,即.xls文件,其要求单个sheet不超过65535

1 写一个包,包的名字叫Util
2 建立一个模块Excel,Excel.py
3 pip install openpyxl==2.4.5
4 类名:ParseExcel
#实现一个构造函数,参数使用excel的绝对路径,并保存到实例变量中
from openpyxl import *
from openpyxl.styles import Border, Side, Font
import time
import datetime class ParseExcel(object):
def __init__(self,excel_file_path):
self.excel_file_path=excel_file_path #加载这个excel文件,并保存到实例变量self.workbook中
class ParseExcel(object):
def __init__(self,excel_file_path):
self.excel_path=excel_file_path
self.workbook=load_workbook(excel_file_path) #实现self.sheet为excel文件的第一个sheet
class ParseExcel(object):
def __init__(self,excel_file_path):
self.excel_path=excel_file_path
self.workbook=load_workbook(excel_file_path)
self.sheet=self.workbook.active # 通过sheet名称获取sheet对象
def get_sheet_by_name(self, sheet_name):
self.sheet=self.workbook.get_sheet_by_name(sheet_name)
return self.sheet # 通过sheet索引获取sheet对象
def get_sheet_by_index(self, sheet_index):
self.sheet=self.workbook.get_sheet_by_index(sheet_index)
return self.sheet # 设置当前要操作的sheet对象,使用index来获取相应的sheet
def set_sheet_by_index(self, sheet_index):
self.sheet=self.workbook.get_sheet_by_index(sheet_index) # 设置当前要操作的sheet对象,使用sheet名称来获取相应的sheet
def set_sheet_by_name(self, sheet_name):
self.sheet=self.workbook.get_sheet_by_name(sheet_name) # 获取默认sheet中最大行数
def get_max_row_no(self):
return self.sheet.max_row # 获取默认 sheet 的最大列数
def get_max_col_no(self):
return self.sheet.max_column # 获取默认sheet的最小(起始)行号
def get_min_row_no(self):
return self.sheet.min_row # 获取默认sheet的最小(起始)列号
def get_min_col_no(self):
return self.sheet.min_column # 获取默认 sheet 的所有行对象,
def get_all_rows(self):
#rows=[]
#for row in self.sheet.iter_rows():
#rows.append(row)
#return rows self.sheet=self.sheet.rows()
return list(self.sheet) # 获取默认sheet中的所有列对象
def get_all_cols(self):
#cols=[]
#for cloumn in self.sheet.iter_cols():
#cols.append(column)
#return cols self.sheet=self.sheet.columns()
return list(self.sheet) # 从默认sheet中获取某一行,第一行从0开始
def get_single_row(self, row_no):
return self.sheet.get_all_rows()[row_no] # 从默认sheet中获取某一列,第一列从0开始
def get_single_col(self, col_no):
return self.sheet.get_all_cols()[col_no] # 从默认sheet中,通过行号和列号获取指定的单元格,注意行号和列号从1开始
def get_cell(self, row_no, col_no):
return self.sheet.cell(row=row_no,column=col_no) # 从默认sheet中,通过行号和列号获取指定的单元格中的内容,注意行号和列号从1开始
def get_cell_content(self, row_no, col_no):
return self.sheet.cell(row=row_no,column=col_no).value # 从默认sheet中,通过行号和列号向指定单元格中写入指定内容,注意行号和列号从1开始
# 调用此方法的时候,excel不要处于打开状态
def write_cell_content(self, row_no, col_no, content, font=None):
self.sheet.cell(row=row_no,column=col_no,value=content,font=None)
self.workbook.save(self.excel_file_path) # 从默认sheet中,通过行号和列号向指定单元格中写入当前日期,注意行号和列号从1开始
# 调用此方法的时候,excel不要处于打开状态
def write_cell_current_time(self, row_no, col_no):
self.sheet.cell(row=row_no,column=col_no,value=time.strftime("%Y:%m:%d",time.localtime()),font=None)
self.workbook.save(self.excel_file_path) def save_excel_file(self):
self.wrokbook.save(self.excel_file_path) if __name__=="__main__":
pe=ParseExcel("c:\\excel.xlsx")
pe.set_sheet_by_index(0)
#print pe.get_default_name()
pe.set_sheet_by_name("Sheet2")
#print pe.get_default_name()
print pe.get_sheet_by_name("Sheet2")
print pe.get_sheet_by_index(0)
print 'max row:', pe.get_max_row_no()
print pe.get_all_rows() # 获取所有行对象
print pe.get_all_rows()[0] # 获取第一行,获取某个行对象
print pe.get_all_rows()[0][1] # 获取第一行,第二列的单元格对象
print pe.get_all_rows()[0][1].value # 获取第一行,第二列单元格对象的值 print pe.get_all_cols() # 获取所有行对象
print pe.get_all_cols()[0] # 获取第一列,获取某个列对象
print pe.get_all_cols()[0][1] # 获取第一列的第二个单元格某个单元格对象
print pe.get_all_cols()[0][1].value # 获取第一列的第二个单元格的值 print pe.get_max_col_no()
print pe.get_min_row_no()
print pe.get_min_col_no()
print pe.get_all_rows()[0]
print len(pe.get_all_rows())
for cell in pe.get_all_rows()[0]:
print cell.value
print pe.get_all_cols()
for col in pe.get_all_cols()[0]:
print col.value
for cell in pe.get_single_row(0):
print cell.value
for col in pe.get_single_col(0):
print col.value
print pe.get_cell(1, 1)
print pe.get_cell_content(1, 1) pe.write_cell_content(4, 2, "wangjing")
print pe.get_cell_content(4, 2)
pe.write_cell_current_time(1, 1)
print pe.get_cell_content(1, 1)

搭建数据驱动框架第一步-实现一个构造函数,将对Excel文件的基本操作API都封装进去的更多相关文章

  1. [Angularjs] 第一步开始一个项目

    [Angularjs] 第一步开始一个项目 一.什么是angularjs angularjs是2009年兴起的,目前由Google维护一个采用mvc模式的js框架,很多时候用来创建单页面应用.我也经常 ...

  2. 零元学Expression Blend 4 - Chapter 3 熟悉操作第一步(制作一个猴子脸)

    原文:零元学Expression Blend 4 - Chapter 3 熟悉操作第一步(制作一个猴子脸) 本篇内容会教你如何使用笔刷.钢笔.渐层以及透明度的调整,还有如何转化图层和路径,最重要的是要 ...

  3. XLConnect:一个用R处理Excel文件的高效平台

    code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...

  4. 如何用 php 读取一个很大的 excel 文件。

    这个程序是用php 读取一个很大的excel文件, 先将 excel 文件保存成csv 文件, 然后利用 迭代器 逐行读取 excel 单元格的值, 拿到值以后 做相应处理,并打印结果. <?p ...

  5. (大数据工程师学习路径)第一步 Linux 基础入门----目录结构及文件基本操作

    Linux 目录结构及文件基本操作 介绍 1.Linux 的文件组织目录结构. 2.相对路径和绝对路径. 3.对文件的移动.复制.重命名.编辑等操作. 一.Linux 目录结构 在讲 Linux 目录 ...

  6. springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

    自己从事java开发工作也有一年多了,自己却没有亲手搭建一个完整的框架.于是今天自己动手搭建一个,过程中遇到一些问题,倒腾了大半天终于搞定了. 现在给大家分享一下过程,自己也记录下来,以后学习参考使用 ...

  7. Yii框架第一步-- 安装

    0.首次安装请看 这里 下面的为非首次安装,不需要token的步骤 1.下载composer 官网下载: https://getcomposer.org/download/ 2.开启PHP的opens ...

  8. None.js 第一步 开启一个服务 hello world

    引入 http 模块 var http = require('http'); 创建服务器 http.createServer(function (request, response) { // 发送一 ...

  9. A框架第一步,传递不同参数.主程序执行对应方法

    访问: www.test.com/admin 1============后台目录:admin (确保单一入口) --有入口文件index.php <?phprequire '../A/a.php ...

随机推荐

  1. DataTable插件报错:Uncaught TypeError: Cannot read property 'style' of undefined

    DataTable插件报错:Uncaught TypeError: Cannot read property 'style' of undefined 原因:table 中定义的列和aoColumns ...

  2. 模块 import 与from

    什么是模块:就是一系列功能的集合体 模块的来源 :1内置模块   2 第三方模块   3 自定义模块 模块的格式: 1 使用python编写的.py文件 2 已被编译为共享库或DLL的C或C++扩展 ...

  3. mysql 主键外键

    外键MUL:一个特殊的索引,用于关键2个表,只能是指定内容 主键PRI:唯一的一个不重复的字段.   # 创建一个表用来引用外键 create table class( -> id int no ...

  4. 用python画小王八裤(turtle库)

    一,采用Python语言如何画一朵玫瑰花 工具/原料 Python语言包 Win10 一. 准备 1. 打开界面: 打开python 2. 创建文件 二. 编程 1. 编写画图: from turtl ...

  5. Docker镜像Push到DockerHub

    1.自己整理的容器首先通过commit做成本地镜像 docker commit -a "Cristin" -m "测试开发平台Jenkins" 281eef85 ...

  6. Java使用Socket进行通信

    什么是Socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket.通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,可以 ...

  7. Zabbix4.0+第三方报警平台OneAlert监控报警

    1. 前言 告警将重要信息发送给运维「或者其他相关人」,及时发现并且处理问题.在所有开源监控软件里面,Zabbix 的告警方式无疑是最棒的.告警的方式各式各样,从 Email 告警到飞信.139/18 ...

  8. [Oracle] 使用PL/SQL Developer 连接远程数据库

    1.在登录界面选择: Database改成xxx.xxx.xxx.xxx/ORCL,如果数据库不是默认的ORCL,那么就改成相应的数据库名. 2.如果失败: 修改ORACLE安装目录下的\produc ...

  9. C# 关键字this用法

    1.this代表当前类的实例对象 public class Test { public string Name{get;set;} public void TestChange(string strN ...

  10. 『TensorFlow』流程控制

    『PyTorch』第六弹_最小二乘法对比PyTorch和TensorFlow TensorFlow 控制流程操作 TensorFlow 提供了几个操作和类,您可以使用它们来控制操作的执行并向图中添加条 ...