python3-常用模块之openpyxl(2)封装
简单封装了下openpyxl,仅供参考,openpyxl版本2.6.2
#操作存在的文件
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.styles import colors
from openpyxl.styles import Font class ParseExcel:
# 初始化表格
def __init__(self,excel_file_path):
self.excel_file_path = excel_file_path
self.wb = load_workbook(excel_file_path)
self.ws = self.wb.active
# 通过sheet名获取sheet表
def get_sheet_by_name(self,sheet_name):
self.ws = self.wb.get_sheet_by_name(sheet_name)
# sheet = self.wb[sheet_name] # 方式二也可以
return self.ws
# 通过索引获取sheet表
def get_sheet_by_index(self,index):
sheet_name = self.wb.sheetnames[index-1]
self.ws = self.wb[sheet_name]
return self.ws
# 获取所有的sheet名,列表格式
def get_all_sheet_names(self):
return self.wb.sheetnames
# 获取单元格数据
def get_cell_value(self,row_no,col_no,sheet_name=None):
if sheet_name == None:
return self.ws.cell(row=row_no, column=col_no).value
return self.wb[sheet_name].cell(row=row_no, column=col_no).value
# 获取单元格对象
def get_cell_obj(self,row_no,col_no,sheet_name=None):
if sheet_name == None:
return self.ws.cell(row=row_no, column=col_no)
return self.wb[sheet_name].cell(row=row_no, column=col_no)
# 获取最大行号
def get_max_row_num(self,sheet_name=None):
if sheet_name == None:
return self.ws.max_row
return self.get_sheet_by_name(sheet_name).max_row
# 获取最大列号
def get_max_col_num(self,sheet_name=None):
if sheet_name == None:
return self.ws.max_column
return self.get_sheet_by_name(sheet_name).max_column
# 获取最小行号,默认为1
def get_min_row_num(self,sheet_name=None):
if sheet_name == None:
return self.ws.min_row
return self.get_sheet_by_name(sheet_name).min_row
# 获取最小列号,默认为1
def get_min_col_num(self,sheet_name=None):
if sheet_name == None:
return self.ws.min_column
return self.get_sheet_by_name(sheet_name).min_column
# 获取某行的值,rows中的行以0开始
def get_some_row_value(self,row_no,sheet_name=None):
row_value = []
if sheet_name is not None:
self.get_sheet_by_name(sheet_name)
for i in list(self.ws.rows)[row_no-1]:
row_value.append(i.value)
return row_value
# 获取某列的值,columns中的行以0开始
def get_some_col_value(self,col_no,sheet_name=None):
col_value = []
if sheet_name is not None:
self.get_sheet_by_name(sheet_name)
for i in list(self.ws.columns)[col_no-1]:
col_value.append(i.value)
return col_value
#保存单元格
def save_excel(self):
self.wb.save(self.excel_file_path)
#单元格写入内容
def write_cell_value(self,row_no,col_no,value,style=None,sheet_name=None):
if sheet_name is not None:
self.get_sheet_by_name(sheet_name)
if style == None:
style = colors.BLACK
elif style.upper() == "RED":
style = colors.RED
elif style.upper() == "GREEN":
style = colors.GREEN
self.ws.cell(row=row_no, column=col_no).font=Font(color=style)
self.ws.cell(row=row_no,column=col_no).value = value
self.save_excel()
return True if __name__ == "__main__":
pe = ParseExcel("sample_demo.xlsx")
# ws = pe.get_sheet_by_name('表1')
# print(pe.get_cell_value(1,1,'表1'))
# print(pe.get_cell_obj(1,1,'表1'))
# print(pe.get_cell_obj(1,1))
pe.write_cell_value(10,10,"nihao",style="red",sheet_name="表1")
pe.write_cell_value(10,11,"钉钉",style="green",sheet_name="表1")
python3-常用模块之openpyxl(2)封装的更多相关文章
- python3 常用模块详解
这里是python3的一些常用模块的用法详解,大家可以在这里找到它们. Python3 循环语句 python中模块sys与os的一些常用方法 Python3字符串 详解 Python3之时间模块详述 ...
- python3 常用模块
一.time与datetime模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们 ...
- Python3常用模块的安装
1.mysql驱动:mysql-connector-python 1.安装 $ pip3 install mysql-connector-python --allow-external mysql-c ...
- Python3 常用模块3
目录 numpy模块 创建numpy数组 numpy数组的属性和用法 matplotlib模块 条形图 直方图 折线图 散点图 + 直线图 pandas模块 numpy模块 numpy模块可以用来做数 ...
- Python3 常用模块2
目录 time 模块 时间戳形式 格式化时间 结构化时间 time.time() time.sleep() datetime 模块 random 模块 hashlib 模块 和 hmac 模块 typ ...
- Python3 常用模块1
目录 os模块 对文件夹操作 对文件进行操作 sys模块 json 和pickle模块 logging模块 日志等级 longging模块的四大组件 自定义配置 os模块 通过os模块我们可以与操作系 ...
- Python3基础(5)常用模块:time、datetime、random、os、sys、shutil、shelve、xml处理、ConfigParser、hashlib、re
---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ 1 ...
- 常用模块 - openpyxl模块
一.简介 xlrd/xlwt 主要是针对Office 2003或更早版本的XLS文件格式 缺点:不支持XLSX文件格式 OpenPyXL 能读能写能修改 缺点:不支持XLS Microsoft Exc ...
- Python3基础笔记--常用模块
目录: 参考博客:Python 之路 Day5 - 常用模块学习 Py西游攻关之模块 一.time模块 二.random模块 三.os模块 四.sys模块 五.hashlib模块 六.logging模 ...
随机推荐
- scrapy 多个爬虫运行
from scrapy import cmdline import datetime import time import os import scrapy from scrapy.crawler i ...
- git mac安装
1.git安装包安装 去官网下载最行的git版本 安装即可 https://git-scm.com/download/mac 但是一般的git仓库需要sshkey来做验证 下面奉上具体的命令: 需要生 ...
- Centos 6.5 python版本升级到2.7.8
Centos6.5默认的 python版本是2.6 为了使用aliyuncli工具,直接用pip安装aliyuncli提示错误. 所以决定升级下python版本,但是yum依赖于python2.6,升 ...
- leetcode-973-最接近原点的K个点
题目描述: 可参考:题215 方法一:排序 class Solution: def kClosest(self, points: List[List[int]], K: int) -> List ...
- System.Drawing.Graphics.cs
ylbtech-System.Drawing.Graphics.cs 1.程序集 System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKey ...
- HttpUrlConnection使用详解--转AAAAA
http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/HttpConnection.html HttpUr ...
- POJ-2499-Binary Tree-思维题
Background Binary trees are a common data structure in computer science. In this problem we will loo ...
- 4_1.springboot2.xWeb开发使用thymeleaf
1.简介 如果使用SpringBoot: 1).创建SpringBoot应用,选中我们需要的模块: 2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运行起来: ...
- 炮兵阵地 /// 状压DP oj26314
题目大意: 炮兵阵地 设置炮兵的位置 其上两位 下两位 左两位 右两位 不能同时设置炮兵 这题是 corn fields玉米地 的升级版 可以先看下这题的注释 更详细些 第一种方法是网上大多数题解的解 ...
- Nand Flash 控制器中的硬件 ECC 介绍
ECC 产生方法 ECC 是用于对存储器之间传送数据正确进行校验的一种算法,分硬件 ECC 和软件 ECC 算法两种,在 S3C2410 的 Nand Flash 控制器中实现了由硬件电路(ECC 生 ...