本文介绍使用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实现替换特定内容的更多相关文章

  1. python - 操作excel表格

    说明:由于公司oa暂缺,人事妹子在做考勤的时候,需要通过几个excel表格去交叉比对员工是否有旷工或迟到,工作量大而且容易出错. 这时候it屌丝的机会来啦,花了一天时间给妹子撸了一个自动化脚本. 1. ...

  2. python操作excel表格(xlrd/xlwt)

    最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...

  3. Python操作excel表格

    用Python操作Excel在工作中还是挺常用的,因为毕竟不懂Excel是一个用户庞大的数据管理软件 注:本篇代码在Python3环境下运行 首先导入两个模块xlrd和xlwt,xlrd用来读取Exc ...

  4. 【转】python操作excel表格(xlrd/xlwt)

    [转]python操作excel表格(xlrd/xlwt) 最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异, ...

  5. python基础(六)python操作excel

    一.python操作excel,python操作excel使用xlrd.xlwt和xlutils模块,xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的 ...

  6. python学习笔记(八)python操作Excel

    一.python操作excel,python操作excel使用xlrd.xlwt和xlutils模块,xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的 ...

  7. python操作excel xlrd和xlwt的使用

    最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...

  8. python 操作excel 的包 函数

    ###########sample 1 https://blog.csdn.net/chengxuyuanyonghu/article/details/54951399 python操作excel主要 ...

  9. Python 操作excel day5

    一.Python操作excel python操作excel使用xlrd.xlwt和xlutils模块 1.xlrd模块是读取excel的: 2.xlwt模块是写excel的: 3.xlutils是用来 ...

随机推荐

  1. Nginx访问路径添加密码保护

    创建口令文件 用openssl命令创建口令 openssl passwd -apr1 会产生一个hash口令, 然后和用户名一起, 以[用户名]:[hash口令]的格式写入文本文件即可 例如创建一个名 ...

  2. java8之Spliterator

    基本用法: import java.util.Arrays; import java.util.Spliterator; import java.util.stream.IntStream; publ ...

  3. Spring cloud微服务安全实战-8-1课程总结

    总结 首先讲了api的安全.安全常见的风险.安全措施.然后我们把简单的api演化成一个这种微服务的架构. 首先讲了在网关上可以做哪些安全的措施.然后讲了如何搭建一个安全中心,也就是认证服务器,包括一些 ...

  4. 解决EasyDSS、EasyNVR流媒体RTMP、HLS(m3u8)、HTTP-FLV播放提示H5播放错误的问题

    背景介绍 EasyDSS流媒体解决方案提供的是一站式的转码.点播.直播.录像.检索.时移回放服务,它的出现极大地简化了开发和集成的工作,基于其强大的后台管理能力,支持多种特性需求,完全能够满足企业视频 ...

  5. alertmanager,grafana,prometheus

    https://zhuanlan.zhihu.com/p/43637757 https://www.cnblogs.com/xiangsikai/p/11289966.html dashboard分文 ...

  6. Windows系统因“CredSSP加密Oracle修正”无法远程连接

    解决办法如下: 在电脑本机运行(快捷键 Win+R)输入:gpedit.msc 回车: 计算机配置->管理模板->系统->凭据分配->右侧找到“加密Oracle凭据”双击-&g ...

  7. [LeetCode] 505. The Maze II 迷宫 II

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  8. 超好用的K8s诊断工具:kubectl-debug

    在K8s环境部署应用后,经常遇到需要进入pod进行排错.除了查看pod logs和describe方式之外,传统的解决方式是在业务pod基础镜像中提前安装好procps.net-tools.tcpdu ...

  9. elasticsearch的数据写入流程及优化

    Elasticsearch 写入流程及优化 一. 集群分片设置:ES一旦创建好索引后,就无法调整分片的设置,而在ES中,一个分片实际上对应一个lucene 索引,而lucene索引的读写会占用很多的系 ...

  10. java有包名的调用没有包名的类,用反射

    没有包名,就是说在根目录,普通项目就是在src下,maven项目就是在src/java目录下 // 通过全类名,没有包名就直接是类名,有包名就要加上包名,比如:com.xiaostudy.TLStri ...