封装Excel操作方法:

先装openpyxl:pip install openpyxl==2.4.5(可以指定版本)

封装脚本:

#encoding=utf-8

from openpyxl import load_workbook

from openpyxl.styles import Border,Side,Font

import time

class parseExcel(object):

def __init__(self,excelPath):

self.excelPath=excelPath

self.workbook = load_workbook(excelPath)#加载excel

self.sheet=self.workbook.active#获取第一个sheet

self.font=Font(color=None)

self.colorDict={"red":'FFFF3030',"green":'FF008B00'}

#设置当前要操作的sheet对象,使用index来获取相应的sheet

def set_sheet_by_index(self,sheet_index):

sheet_name=self.workbook.get_sheet_names()[sheet_index]

self.sheet=self.workbook.get_sheet_by_name(sheet_name)

return self.sheet

#获取当前默认sheet的名字

def get_default_sheet(self):

return self.sheet.title

# 设置当前要操作的sheet对象,使用sheet名称来获取相应的sheet

def set_sheet_by_name(self,sheet_name):

sheet=self.workbook.get_sheet_by_name(sheet_name)

self.sheet=sheet

return self.sheet

#获取默认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):

return list(self.sheet.iter_rows())

#return list(self.rows)也可以

#获取默认sheet中的所有列对象

def get_all_cols(self):

return list(self.sheet.iter_cols())

#return list(self.sheet.columns)也可以

#从默认sheet中获取某一列,第一列从0开始

def get_single_col(self,col_no):

return self.get_all_cols()[col_no]

#从默认sheet中获取某一行,第一行从0开始

def get_single_row(self,row_no):

return self.get_all_rows()[row_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

self.workbook.save(self.excelPath)

return self.sheet.cell(row=row_no,column=col_no).value

# 从默认sheet中,通过行号和列号向指定单元格中写入当前日期,注意行号和列号从1开始

#调用此方法的时候,excel不要处于打开状态

def write_cell_current_time(self,row_no,col_no):

time1=time.strftime("%Y-%m-%d %H:%M:%S")

self.sheet.cell(row=row_no,column=col_no).value=str(time1)

self.workbook.save(self.excelPath)

return self.sheet.cell(row=row_no,column=col_no).value

def save_excel_file(self):

self.workbook.save(self.excelPath)

if __name__=='__main__':

p=parseExcel(u'D:\\testdata.xlsx')

print u"获取默认行:",p.get_default_sheet()

print u"设置sheet索引为1",p.set_sheet_by_index(1)

print u"获取默认行:",p.get_default_sheet()

print u"设置sheet索引为0",p.set_sheet_by_index(0)

print u"获取默认行:",p.get_default_sheet()

#for i in range(3,6):

#for j in range(3,6):

#p.write_cell_content(i,j,str((i,j)))

print u"最大行数:",p.get_max_row_no()

print u"最大列数:",p.get_max_col_no()

print u"最小起始行数:",p.get_min_row_no()

print u"最小起始列数:",p.get_min_col_no()

print u"所有行对象:",p.get_all_rows()

print u"所有列对象:",p.get_all_cols()

print u"获取某一列(2):",p.get_single_col(2)

print u"获取某一行(4):",p.get_single_row(4)

print u"取得行号和列号(2,2)单元格:",p.get_cell(2,2)

print u"取得行号和列号单元格的内容(2,2)",p.get_cell_content(2,2)

print u"行号和列号写入内容(11,11):'xiaxiaoxu'",p.write_cell_content(11,11,'xiaxiaoxu')#

print u"行号和列号写入当前日期(13,13):",p.write_cell_current_time(13,13)

testdata.xlsx运行前:

结果:

D:\test>python test2.py

获取默认行:  aone

设置sheet索引为1

获取默认行: two

设置sheet索引为0

获取默认行:  aone

最大行数: 13

最大列数: 13

最小起始行数: 3

最小起始列数: 3

所有行对象: [(, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , )]

所有列对象: [(, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , ), (, , , , , , , , , , , , )]

获取某一列(2): (, , , , , , , , , , , , )

获取某一行(4): (, , , , , , , , , , , , )

取得行号和列号(2,2)单元格:

取得行号和列号单元格的内容(2,2) None

行号和列号写入内容(11,11):'xiaxiaoxu' xiaxiaoxu

行号和列号写入当前日期(13,13): 2018-07-03 22:21:18

Testdata.xlsx运行后:

注意点:

self.sheet.active()获取第一个sheet,这个第一个指的sheet名的ascii码第一个字母排在最前的

不熟悉的话可以在命令行界面自己练习一下,用dir()命令查看workbook和sheet的常用的方法:

>>> from openpyxl import *

>>> wb=load_workbook('d:\\testdata.xlsx')

>>> wb.get_sheet_names()

[u' aone', u'two', u'sheet3']

>>> sheet1=wb.active

>>> sheet1

>>> sheet1.max_column

8

>>> sheet1.max_row

8

>>> sheet1.min_row

3

>>> sheet1.min_column

3

>>> sheet1.rows

>>> sheet1.iter_rows()

>>> sheet1.columns

>>> dir(wb)

['_Workbook__write_only', '__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_active_sheet_index', '_add_sheet', '_alignments', '_borders', '_cell_styles', '_colors', '_data_only', '_differential_styles', '_external_links', '_fills', '_fonts', '_keep_links', '_named_styles', '_number_formats', '_protections', '_read_only', '_setup_styles', '_sheets', 'active', 'add_named_range', 'add_named_style', 'chartsheets', 'close', 'code_name', 'copy_worksheet', 'create_chartsheet', 'create_named_range', 'create_sheet', 'data_only', 'defined_names', 'encoding', 'excel_base_date', 'get_active_sheet', 'get_index', 'get_named_range', 'get_named_ranges', 'get_sheet_by_name', 'get_sheet_names', 'guess_types', 'index', 'is_template', 'keep_links', 'loaded_theme', 'mime_type', 'named_styles', 'path', 'properties', 'read_only', 'rels', 'remove', 'remove_named_range', 'remove_sheet', 'save', 'security', 'shared_strings', 'sheetnames', 'style_names', 'template', 'vba_archive', 'worksheets', 'write_only']

python封装为dll excel_python openpyxl 封装Execl常用操作的方法相关推荐

  1. Python字典dict的增删查改及常用操作

    字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据.python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必须是可 ...

  2. day03 Python字典dict的增删查改及常用操作

    字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据.python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必须是可 ...

  3. python安卓脚本 模拟滑动_python模拟鼠标拖动操作的方法

    本文实例讲述了python模拟鼠标拖动操作的方法.分享给大家供大家参考.具体如下: pdf中的书签只有页码,准备把现有书签拖到一个目录中,然后添加自己页签.重复的拖动工作实在无趣,还是让程序帮我实现吧 ...

  4. python写下拉列表在excel_python – OpenPyXL:是否可以在Excel工作表中创建下拉菜单?...

    我正在尝试使用openpyxl在单元格中存储有效IP地址列表.目前,数据只是放入一个单元格,通常会溢出到其他单元格中.使用以下代码: # Regex to return a tidy list of ...

  5. python把数据写入excel_Python向excel中写入数据的方法

    Python向excel中写入数据的方法 最近做了一项工作需要把处理的数据写入到Excel表格中进行保存,所以在此就简单介绍使用Python如何把数据保存到excel表格中. 数据导入之前需要安装 x ...

  6. python定时任务巡检写入excel_Python巡检关于Excel表格操作

    importpsutilimporttimeimportxlwtimportplatformfrom subprocess importPopen, PIPEdefgetoutput(command) ...

  7. 【Python】图片视频处理常用操作及方法

    文章目录 1. 图片处理操作 ⭐ 图片尺寸获取 ⭐ 图片resize ⭐ 数组转化为图片 ⭐ 图片保存 ⭐ 画矩形 ⭐ 写文本 2. 视频处理操作 ⭐ 视频信息获取 ⭐ 视频resize及保存 1. ...

  8. python excel提取 替换_python代替excel的常用操作

    numpy常用函数 %matplotlib notebook import matplotlib.pyplot as plt import numpy as np x1=np.arange(100)# ...

  9. Python自学教程5-字符串有哪些常用操作

    任何编程语言,不管是Python.Java 还是 Golang, 字符串都是最重要的一种数据类型. 但是字符串的操作又很多,初学者经常毫无头绪,不知道从哪儿学起,也不知道哪些操作用得多,今天九柄就和你 ...

最新文章

  1. 与微信、APP正面刚?三大运营商联合发布5G消息白皮书
  2. NetApp SE 实验室报告:SAN Boot with VMware ESX 3.0.0
  3. 21行代码AC——HDU1106 排序
  4. 2021年高考成绩查询襄阳状元,大胆猜测一下,2021年高考,湖北省文理状元会花落谁家?...
  5. [BZOJ1509][NOI2003]逃学的小孩
  6. hdu 2048 神、上帝以及老天爷
  7. postman生成python代码_别再用手敲了,这个工具可以自动生成python爬虫代码
  8. SQL server 2005安装问题汇总
  9. Git的安装以及简单使用
  10. 三十岁左右的你正处于什么状态?
  11. 重磅!谷歌Fuchsia操作系统将支持运行Linux应用程序
  12. 类和对象编程(一):类成员函数
  13. NOD32企业内部更新服务器搭建
  14. android6刷机教程,安卓手机刷机步骤
  15. 圣经中真的藏有密码吗? 摘自台湾权威杂志《科学月刊》
  16. PLM Agile 随笔
  17. IPAM——IP地址管理
  18. Excel PivotTable 使用心得手顺分享(五)
  19. java js方法_java如何调用js方法
  20. 自己动手实现主题搜索引擎

热门文章

  1. CMD打开方式+Dos命令
  2. 如何删除cloud这个流氓软件
  3. HoloLens联合发明人:打造理想的全天AR需要解决这些问题
  4. 中断控制器8259——工作方式、命令字
  5. 将UBB代码转换成html代码 转
  6. Vue 3.0 + Element Plus 踩坑
  7. SketchUp Pro 2023草图大师对Mac和Windows的系统要求如下
  8. 几个程序员面试题:火车运煤问题、赛马问题
  9. Python识别验证码----谷歌reCapture 3*3验证码
  10. 二分法 matlab应用,MATLAB算法の二分法