本文介绍使用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 = wb.worksheets[0]
19     global changeCells
20     # get rows and columns of file
21     rows = ws.max_row
22     cols = ws.max_column
23     changeFlag = False
24     try:
25         for row in range(1, rows+1):
26             for col in range(1, cols+1):
27                 content = ws.cell(row=row, column=col).value
28                 if(content != None):
29                     # mode1: fullmatch replacement
30                     if(mode == 1):
31                         if(content == text):
32                             ws.cell(row=row, column=col).value = replaceText
33                             changeFlag = True
34                             changeCells += 1
35                     # mode2: partial replacement
36                     elif(mode == 2):
37                         if(type(content) == str):
38                             ws.cell(row=row, column=col).value = content.replace(
39                                 text, replaceText, 1)
40                             changeFlag = True
41                             changeCells += 1
42                     # mode3: partialmatch and filling
43                     elif(mode == 3):
44                         if(type(content) == str):
45                             ws.cell(row=row, column=col).value = content.replace(
46                                 text, text+replaceText, 1)
47                             changeFlag = True
48                             changeCells += 1
49                     else:
50                         return 0
51         # status_1: modified success
52         if(changeFlag):
53             wb.save(file)
54             return changeCells
55         # status_2: no modified
56         else:
57             return changeCells
58     # status_3: exception
59     except Exception as e:
60         print(traceback.format_exc())
61
62
63 # read the content of file
64 """
65 file: file path : str
66 """
67 def rdxl(file):
68     # load the file(*.xlsx)
69     wb = openpyxl.load_workbook(file)
70     # ! deal with one sheet
71     ws = wb.worksheets[0]
72     global changeCells
73     # get rows and columns of file
74     rows = ws.max_row
75     cols = ws.max_column
76     changeFlag = False
77     cells = 0
78     for row in range(1, rows+1):
79         for col in range(1, cols+1):
80             content = ws.cell(row=row, column=col).value
81             print(content)
82             cells += 1
83     print('cells', cells)
84
85
86 if __name__ == "__main__":
87      res = changeData('D:\\001.xlsx', 1, 7777, 'bug制造者')
88      if(res != None):
89          print('已修改 ', res, ' 处')
90      # else:
91      #     print('操作失败:\n'+res)
92      rdxl('D:\\001.xlsx')

python 操作excel实现替换特定内容相关推荐

  1. python 获取excel文本框_如何基于python操作excel并获取内容

    这篇文章主要介绍了如何基于python操作excel并获取内容,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 背景:从excel表中获取请求url. ...

  2. 一文学会用Python操作Excel+Word+CSV

    作者:奈何缘浅wyj https://juejin.im/post/6868073137263607821 Python 操作 Excel 常用工具 数据处理是 Python 的一大应用场景,而 Ex ...

  3. 全网最全 Python 操作 Excel 教程,建议收藏!

    [欢迎关注微信公众号:厦门微思网络] 微思网络(官网):https://www.xmws.cn/ 0 Python Excel库对比 我们先来看一下python中能操作Excel的库对比(一共九个库) ...

  4. 全网最全Python操作Excel教程,建议收藏!

    作者:超级大洋葱806 来源:https://blog.csdn.net/u014779536/article/details/108182833 大家好,猪哥前几天帮学妹爬了个数据,使用到了Pyth ...

  5. python 操作excel神器_【转】多图+代码 | 详解Python操作Excel神器openpyxl的各种操作!...

    转自:https://blog.csdn.net/weixin_41846769/article/details/108273349 前言 大家好,在之前的十几篇办公自动化系列文章中,我们大多是以真实 ...

  6. python每行输出8个式子_多图+代码 | 详解Python操作Excel神器openpyxl的各种操作!

    前言 大家好,在之前的十几篇办公自动化系列文章中,我们大多是以真实的案例需求来讲解Python如何进行自动化办公操作,并且多次使用到openpyxl来处理表格,今天我们就来详细的盘点Python操作E ...

  7. 全网最全Python操作Excel教程,赶紧收藏

    0 Python Excel库对比 我们先来看一下python中能操作Excel的库对比(一共九个库): 1 Python xlrd 读取 操作Excel 1.1 xlrd模块介绍 (1)什么是xlr ...

  8. 用python实现excel 14个常用操作,用Python 操作 Excel,这篇文章别错过了!(超全总结)...

    在之前的办公自动化系列文章中,我已经对Python操作Excel的几个常用库openpyxl.xlrd/xlwt.xlwings.xlsxwriter等进行了详细的讲解. 为了进一步带大家了解各个库的 ...

  9. 用Python 操作 Excel,这篇文章别错过了!(超全总结)

    在之前的办公自动化系列文章中,我已经对Python操作Excel的几个常用库openpyxl.xlrd/xlwt.xlwings.xlsxwriter等进行了详细的讲解. 为了进一步带大家了解各个库的 ...

最新文章

  1. 怎样查看一个端口有无开启
  2. 第十八课.支持向量机
  3. CV之CycleGAN:CycleGAN算法相关思路配图、论文集合
  4. CompletableFuture的多线程和异步监听实现
  5. SpringMVC 通过post接收form参数或者json参数
  6. Kaldi的英文缩写
  7. bash linux .ee,Linux下Bash shell学习笔记.md
  8. github 分支 合并
  9. css3中3d旋转中rotatex,rotatey,rotatez的旋转正方向
  10. locale的设定及LANG、LC_CTYPE、LC_ALL环境变量
  11. Android热修复Sophix详解
  12. 【腾讯TMQ】老司机教你如何优雅地完成一个小项目测试
  13. 如何提高人际交往的沟通技巧
  14. iqooz6和z5哪个好 iqoo z6和iqoo z5哪个更值得入手
  15. 8、虚拟串口(VSPT)使用小记
  16. 代码中的软件工程:正则表达式十步通关
  17. 吉林大学计算机唐班公示,重要通知|数学学院16级及17级唐敖庆班选拔方案及公示名单...
  18. 【线性代数】线性相关与线性无关的定义与性质
  19. 如何预置Android 手机 APK
  20. 第三天 入口文件index.php 01

热门文章

  1. 为什么要使用IP对讲终端?它有什么优势?
  2. BUG Free的简易安装
  3. html点击导航变色,点击导航后,当前导航的颜色变色
  4. 【校招VIP】产品文案策划之文学常识
  5. 补天 公益src 爬虫
  6. gather torch_我对torch中的gather函数的一点理解
  7. 【渗透实战系列】飞鱼星家庭路由器越权发现的木马传播事件
  8. LAMMPS回到未来计划
  9. IEEE:基于轻量级特征增强卷积神经网络的低空小目标检测
  10. 在 Mac 上将 PDF 转换为 Word 的 10 个免费工具