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是用来 ...
随机推荐
- Zookeeper运维常用四字命令
Zookeeper运维常用四字命令 echo stat|nc 127.0.0.1 2181 查看哪个节点被选择作为follower或者leader 使用echo ruok|nc 127.0.0.1 2 ...
- 001 okhttp3的POST使用
继续使用上面的项目 1.被调用的项目 package com.jun.web2forokhttp.okhttp; import com.jun.web2forokhttp.bean.HttpDomai ...
- (转)IIS windows认证
转自 https://my.oschina.net/u/2551141/blog/2878673 IIS配置Windows认证方式: 1.IIS->>要设置的网站->>身份验证 ...
- IISExpress.无法启动IIS Express Web 服务器.Starting IIS Express... IIS Express is running
x 提示: 无法启动IIS Express Web 服务器. 来自IIS Express的输出: Starting IIS Express... IIS Express is running 总结: ...
- LODOP表格table简短问答及相关博文
LODOP打印表格超文本输出表格:ADD_PRINT_HTML.ADD_PRINT_HTM.ADD_PRINT_TABLE.ADD_PRINT_TBURL打印表格带页头页尾 参考样例15 http:/ ...
- js 类型判断
- [LeetCode] 676. Implement Magic Dictionary 实现神奇字典
Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...
- LeetCode:第K个排列【60】
LeetCode:第K个排列[60] 题目描述 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: &quo ...
- jenkins:新增节点是启动方式没有Launch agent by connecting it to the master
默认在这里的配置是禁用 所以启动方式只有两种,缺少Launch agent by connecting it to the master
- 在CentOS 7中 使用 Nginx 反代 .Net Core
很久没弄 .Net Core 了,然后忽然发现Windows自带的 Hyper-V 虚拟机貌似挺好用的 .Net Core 之前都是用 Jexus 来做服务器,忽然想用下Nginx来试试 1.在 Ce ...