本文介绍使用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. git 项目最常用命令总结

    本文为博主原创,未经允许不得转载: 1.查看git基础配置信息 1.查看用户名和邮箱地址 git config user.name   git config user.email 2.修改用户名和邮箱 ...

  2. AnnotatedElementUtils.findMergedAnnotation作用

    // 在element上查询annotationType类型注解 // 将查询出的多个annotationType类型注解属性合并到查询的第一个注解中 // # 多个相同注解合并 org.spring ...

  3. Wise Force Deleter 强制删除文件工具

    https://www.xitmi.com/3321.html   Wise Force Deleter v1.49 中文绿色版 强制删除文件工具

  4. centos6.10中部署percona-mysql双实例的方法

    centos6.10中部署percona-mysql双实例的方法 数据库的同步关系:192.168.11.53(master) --> slave(192.168.10.189) --> ...

  5. 转 Shell判断字符串包含关系的几种方法

    https://blog.csdn.net/rznice/article/details/71086839 Shell中判断字符串包含关系的方法: 1.通过grep来判断:12str1="a ...

  6. python random模块(14)

    random 模块包括返回随机数的函数,可以用于模拟或者任何产生随机输出的程序. 一.random模块常用函数介绍 random.random() — 生成一个从0.0(包含)到 1.0(不包含)之间 ...

  7. hashMap常见问题

    [解析hashMap的源码实现]      点击进入hashMap的源码实现 0.谈谈对hashMap的理解? 从底层结构.存取.扩容.冲突.实现原理.源码等方面说明. 1.你知道哪些常用的Map集合 ...

  8. LeetCode 32. 最长有效括号(Longest Valid Parentheses) 31

    32. 最长有效括号 32. Longest Valid Parentheses 题目描述 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 每日一算法2019/6/ ...

  9. Mysql中的读锁,写锁,乐观锁及事务隔离级别和并发问题

    mysql读锁,写锁,乐观锁 读锁,也叫共享锁(shared lock) SELECT * FROM table_name  WHERE ...  LOCK IN SHARE MODE 写锁,也叫排他 ...

  10. 使用Xshell采用证书登录Linux

    1,工具--- 用户key生成向导,选择秘钥类型,注意DSA只能选择1024位级以下,超过1024位的服务器不认. 2 给公钥取一个名字,并设置密码,保存为文件 3 在linux 用户目录,用cd  ...