本文介绍使用python语言,借助openyxl库来实现操作excel(xlsx)文件,实现替换特定内容的需求。
目前实现了3个小功能:
1. 全字匹配替换(mode1);(如:全字匹配 yocichen, 替换成为 yociXchen)
2. 部分字符匹配替换(mode2);(如:thisisyociblog,替换成为 thisisyocichenblog)
3. 全字匹配填充(mode3);(如:yoci,替换成为yoci: a foolish),用于在字符后面添加字符
源码:
1 import openpyxl 2 import re 3 import traceback 4 5 changeCells = 0 6 7 # replace the special content 8 """ 9 file: file path : str 10 mode: type of the operatoration : int 11 text: the string need to be replaceed : int or str 12 replaceText: replacement Text : int or str 13 """ 14 def changeData(file, mode, text, replaceText): 15 # load the file(*.xlsx) 16 wb = openpyxl.load_workbook(file) 17 # ! deal with one sheet 18 ws =