python 操作excel实现替换特定内容
本文介绍使用python语言,借助openyxl库来实现操作excel(xlsx)文件,实现替换特定内容的需求。
目前实现了3个小功能:
1. 全字匹配替换(mode1);(如:全字匹配 yocichen, 替换成为 yociXchen)
2. 部分字符匹配替换(mode2);(如:thisisyociblog,替换成为 thisisyocichenblog)
3. 全字匹配填充(mode3);(如:yoci,替换成为yoci: a foolish),用于在字符后面添加字符
源码:
import openpyxl
import re
import traceback changeCells = 0 # replace the special content
"""
file: file path : str
mode: type of the operatoration : int
text: the string need to be replaceed : int or str
replaceText: replacement Text : int or str
"""
def changeData(file, mode, text, replaceText):
# load the file(*.xlsx)
wb = openpyxl.load_workbook(file)
# ! deal with one sheet
ws = wb.worksheets[0]
global changeCells
# get rows and columns of file
rows = ws.max_row
cols = ws.max_column
changeFlag = False
try:
for row in range(1, rows+1):
for col in range(1, cols+1):
content = ws.cell(row=row, column=col).value
if(content != None):
# mode1: fullmatch replacement
if(mode == 1):
if(content == text):
ws.cell(row=row, column=col).value = replaceText
changeFlag = True
changeCells += 1
# mode2: partial replacement
elif(mode == 2):
if(type(content) == str):
ws.cell(row=row, column=col).value = content.replace(
text, replaceText, 1)
changeFlag = True
changeCells += 1
# mode3: partialmatch and filling
elif(mode == 3):
if(type(content) == str):
ws.cell(row=row, column=col).value = content.replace(
text, text+replaceText, 1)
changeFlag = True
changeCells += 1
else:
return 0
# status_1: modified success
if(changeFlag):
wb.save(file)
return changeCells
# status_2: no modified
else:
return changeCells
# status_3: exception
except Exception as e:
print(traceback.format_exc()) # read the content of file
"""
file: file path : str
"""
def rdxl(file):
# load the file(*.xlsx)
wb = openpyxl.load_workbook(file)
# ! deal with one sheet
ws = wb.worksheets[0]
global changeCells
# get rows and columns of file
rows = ws.max_row
cols = ws.max_column
changeFlag = False
cells = 0
for row in range(1, rows+1):
for col in range(1, cols+1):
content = ws.cell(row=row, column=col).value
print(content)
cells += 1
print('cells', cells) if __name__ == "__main__":
res = changeData('D:\\001.xlsx', 1, 7777, 'bug制造者')
if(res != None):
print('已修改 ', res, ' 处')
# else:
# print('操作失败:\n'+res)
rdxl('D:\\001.xlsx')
python 操作excel实现替换特定内容的更多相关文章
- python - 操作excel表格
说明:由于公司oa暂缺,人事妹子在做考勤的时候,需要通过几个excel表格去交叉比对员工是否有旷工或迟到,工作量大而且容易出错. 这时候it屌丝的机会来啦,花了一天时间给妹子撸了一个自动化脚本. 1. ...
- python操作excel表格(xlrd/xlwt)
最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...
- Python操作excel表格
用Python操作Excel在工作中还是挺常用的,因为毕竟不懂Excel是一个用户庞大的数据管理软件 注:本篇代码在Python3环境下运行 首先导入两个模块xlrd和xlwt,xlrd用来读取Exc ...
- 【转】python操作excel表格(xlrd/xlwt)
[转]python操作excel表格(xlrd/xlwt) 最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异, ...
- python基础(六)python操作excel
一.python操作excel,python操作excel使用xlrd.xlwt和xlutils模块,xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的 ...
- python学习笔记(八)python操作Excel
一.python操作excel,python操作excel使用xlrd.xlwt和xlutils模块,xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的 ...
- python操作excel xlrd和xlwt的使用
最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...
- python 操作excel 的包 函数
###########sample 1 https://blog.csdn.net/chengxuyuanyonghu/article/details/54951399 python操作excel主要 ...
- Python 操作excel day5
一.Python操作excel python操作excel使用xlrd.xlwt和xlutils模块 1.xlrd模块是读取excel的: 2.xlwt模块是写excel的: 3.xlutils是用来 ...
随机推荐
- docker vim右键进入visual模式无法粘贴
右键不能粘贴,反而进入了visual模式, vim版本:version 8.0.707 修改方法: vim /usr/share/vim/vim80/defaults.vim 第70行 在mouse= ...
- (八)Knockout 组件 Components
概述 :组件和自定义元素 Components 是将UI代码组织成自包含的.可重用的块的一种强大而干净的方法.他们: -可以表示单个控件/窗口小部件或应用程序的整个部分 -包含它们自己的视图,并且通常 ...
- elementui---for循环需要添加KEY
在用VUE和elementui开发项目的时候,在开启 es-lient 的时候,如果for循环没有添加 key ,会报语法上的错误. genderSelect:[ {value:0,label:'女' ...
- java8(2)--- Stream API
1.简述 Java8中有两大最为重要的改变.第一个是 Lambda 表达式:另外一 个就是 Stream API. Stream 是处理集合的抽象概念,它可以指定你希望对集合进行的操作,可以执行非常复 ...
- Kubernetes 健康状态检查(九)
强大的自愈能力是 Kubernetes 这类容器编排引擎的一个重要特性.自愈的默认实现方式是自动重启发生故障的容器.除此之外,用户还可以利用 Liveness 和 Readiness 探测机制设置更精 ...
- memcached概述与基本操作
memcached 什么是memcached memcached之前是danga的一个项目,最早是为LiveJournal服务的,当初设计师为了加速LiveJournal访问速度而开发的,后来被很多大 ...
- acme自动证书申请
安装acme.sh curl https://get.acme.sh | sh acme.sh默认安装到了当前家目录. [root@iZbp17hycbhnayg00ohec9Z ~]# ~/.acm ...
- poj 2775 文件结构“图"
总时间限制: 1000ms 内存限制: 65536kB 描述 在计算机上看到文件系统的结构通常很有用.Microsoft Windows上面的"explorer"程序就是这样的一个 ...
- 基于thinkphp5框架做一个可以区别开发、测试、生产三种环境的配置加载
在日常的开发测试中我们经常会遇到本地开发和测试或者线上配置参数不同的场景,必要你要是使用一个三方的支付,它的本地测试和线上的key值或者账号存在不同.最基本的做法是本地用测试参数,提交到测试的时候再改 ...
- 在 Mac 系统下安装 PyCharm 的方法
首先,进入 PyCharm 的官网,PyCharm: Python IDE for Professional Developers by JetBrains. 如上图所示,直接点击DOWNLOAD N ...