当前位置: 首页 > news >正文

长春移动网站建设加盟

长春移动网站建设,加盟,黑龙江省建设会计协会网站首页,做响应式的网站一、问题的提出 有时,我们手头上有多个Excel或者Word文件,但是领导突然要求对某几个术语进行批量的修改,你是不是有要崩溃的感觉。因为这么多文件,要一个一个地打开文件,再进行批量替换修改,几个文件还好&…

一、问题的提出

有时,我们手头上有多个Excel或者Word文件,但是领导突然要求对某几个术语进行批量的修改,你是不是有要崩溃的感觉。因为这么多文件,要一个一个地打开文件,再进行批量替换修改,几个文件还好,如果是成百上千的文件,我想你一会儿就感觉自己被搞晕了,不仅搞不清修改了没有修改完,而且已经修改的也不知道修改的彻底不。

于是,问题来了,当我需要对多个Excel和Word文件中的关键字进行替换,而且不改变原文件的格式,同时删除源文件,我们该怎么办?这些office文件可能分布在不同的文件夹下,所以替换后还要存放在原来的文件夹。同时,我们编写的程序还要在Windows和MacOS环境下都可以使用。

二、算法分析

由于要在多个环境下使用,我们放弃VBA,考虑采用Python编程的方法来解决。

1. 第一步 读取一个替换关键字的"批量替换表.xlsx"生成一个字典,这样是为了后面可以批量替换。第二步 遍历当前目录下所有目录包括上当的文件,主要是docx和xlsx文件,如果是doc和xls文件,还要考虑两这两种格式的文件进行批量的转化,见下面的文章 。

批量转doc和xls为docx和xlsx文件

2. 第二步是 遍历当前所有目录中的文件,用if条件,根据文件扩展名的不同来筛选出docx和xlsx文件。代码如下:

    for root, filefolder, files in os.walk(os.curdir):for file in files:if file.endswith("docx"):file_path = os.path.join(root, file)for key, value in dic.items():word_replace_keywords(file_path, key, value)elif file.endswith("xlsx") and os.path.basename(file)!="批量替换表.xlsx":file_path = os.path.join(root, file)for key, value in dic.items():excel_replace_keywords(file_path, key, value)

3. 第三步是对于docx和xlsx文件分别进行替换处理,主要采用了python-docx和openpyxls这两个模块来进行替换。针对docx文件,我们用Document()来读取,用以下代码来替换:

def info_update(doc, old, new):for para in doc.paragraphs:for run in para.runs:if old in run.text:run.text = run.text.replace(old, new)

对于xlsx文件我,我们通过下面的代码实现关键字替换,同时不改变原来关键字的格式。

def replace_cell_text_with_format(cell, keyword, replacement):paragraphs = cell.paragraphsfor paragraph in paragraphs:for run in paragraph.runs:if keyword in run.text:new_text = run.text.replace(keyword, replacement)run.clear()  # 清除当前文本new_run = run._element  # 创建新的runnew_run.text = new_text  # 设置新文本for key in run._r.attrib.keys():  # 复制格式属性if key != 't':new_run.attrib[key] = run._r.attrib[key]

4. 第四步 我们要保存替换后的文件,同时用os.remove()删除原来的文件。

三、代码展示

最终,我们编制出70多行的代码,一键实现了多文件、多关键字、保存源格式,又能在Windows和苹果电脑环境使用的程序。代码如下:

import os
from docx import Document
from openpyxl import load_workbookdef info_update(doc, old, new):for para in doc.paragraphs:for run in para.runs:if old in run.text:run.text = run.text.replace(old, new)def replace_cell_text_with_format(cell, keyword, replacement):paragraphs = cell.paragraphsfor paragraph in paragraphs:for run in paragraph.runs:if keyword in run.text:new_text = run.text.replace(keyword, replacement)run.clear()  # 清除当前文本new_run = run._element  # 创建新的runnew_run.text = new_text  # 设置新文本for key in run._r.attrib.keys():  # 复制格式属性if key != 't':new_run.attrib[key] = run._r.attrib[key]
def get_dic():workbook = load_workbook('批量替换表.xlsx')sht = workbook.activedic = {}for c1,c2 in zip(sht["A"],sht["B"]):if c1.value!= None and c2.value!= None:dic[c1.value] = c2.valuereturn dicdef word_replace_keywords(file_path, keyword, replacement):doc = Document(file_path)info_update(doc, keyword, replacement)try: for table in doc.tables:if not any(cell.text for row in table.rows for cell in row.cells):continue  for row in table.rows:for cell in row.cells:if keyword in cell.text:replace_cell_text_with_format(cell, keyword, replacement)except Exception as e:print("Error processing table:", e)doc.save(file_path)def excel_replace_keywords(file_path, keyword, replacement):wb = load_workbook(file_path)for sheet_name in wb.sheetnames:sheet = wb[sheet_name]for row in sheet.iter_rows():for cell in row:if cell.value and keyword in str(cell.value):cell.value = str(cell.value).replace(keyword, replacement)wb.save(file_path)wb.close()def get_replaced(dic):    for root, filefolder, files in os.walk(os.curdir):for file in files:if file.endswith("docx"):file_path = os.path.join(root, file)for key, value in dic.items():word_replace_keywords(file_path, key, value)elif file.endswith("xlsx") and os.path.basename(file)!="批量替换表.xlsx":file_path = os.path.join(root, file)for key, value in dic.items():excel_replace_keywords(file_path, key, value)
def main():dic = get_dic()get_replaced(dic)
if __name__ == "__main__":main()

以上代码的优势在于:速度快,设置好关键字后一键替换,可以在多个环境下使用,相比VBA代码,Python代码的执行速度更快、操作更简单、省时省力。

四、注意事项

1. 运行代码前一定要安装Python3.9及以上版本,同时安装openpyxl和python-docx两个模块。

2. 执行程序前要把doc和xls文件分别转化为docx和xlsx文件,这样更方便替换。

3. 执行前要在程序文件目录下建立一个xlsx文件,命名为"批量替换表.xlsx",在表的A列放上要查找的关键字,B列放要替换的关键字。

4. 如果有问题,可以随时与我联系,也可以通过下面进行提问。


文章转载自:
http://hydrophilic.c7623.cn
http://epirot.c7623.cn
http://interwoven.c7623.cn
http://umayyad.c7623.cn
http://crupper.c7623.cn
http://excursion.c7623.cn
http://trichinotic.c7623.cn
http://hydroxid.c7623.cn
http://zing.c7623.cn
http://rosiny.c7623.cn
http://talkativeness.c7623.cn
http://damnation.c7623.cn
http://unflawed.c7623.cn
http://lectotype.c7623.cn
http://inswinger.c7623.cn
http://acrid.c7623.cn
http://glanders.c7623.cn
http://kenspeckle.c7623.cn
http://onagraceous.c7623.cn
http://condense.c7623.cn
http://weregild.c7623.cn
http://heroa.c7623.cn
http://bonito.c7623.cn
http://dardan.c7623.cn
http://fantasist.c7623.cn
http://landtied.c7623.cn
http://rationalistic.c7623.cn
http://initiatress.c7623.cn
http://synchromesh.c7623.cn
http://sinologist.c7623.cn
http://marconigram.c7623.cn
http://vega.c7623.cn
http://thermogravimetry.c7623.cn
http://co2.c7623.cn
http://lapp.c7623.cn
http://henchman.c7623.cn
http://tgwu.c7623.cn
http://where.c7623.cn
http://thunderhead.c7623.cn
http://reinsure.c7623.cn
http://boll.c7623.cn
http://sideboard.c7623.cn
http://photoshp.c7623.cn
http://epicyclic.c7623.cn
http://malicious.c7623.cn
http://sportfishing.c7623.cn
http://polytene.c7623.cn
http://frenzy.c7623.cn
http://umbriferous.c7623.cn
http://grit.c7623.cn
http://bondon.c7623.cn
http://demineralize.c7623.cn
http://trawlboat.c7623.cn
http://unwarned.c7623.cn
http://revisional.c7623.cn
http://deoxidation.c7623.cn
http://purplish.c7623.cn
http://amphitheatrical.c7623.cn
http://ytterbium.c7623.cn
http://grime.c7623.cn
http://youngly.c7623.cn
http://hasheesh.c7623.cn
http://tenebrescence.c7623.cn
http://snath.c7623.cn
http://maidhood.c7623.cn
http://auramine.c7623.cn
http://monacal.c7623.cn
http://midnoon.c7623.cn
http://melee.c7623.cn
http://deucalion.c7623.cn
http://deixis.c7623.cn
http://theme.c7623.cn
http://calendarian.c7623.cn
http://interbellum.c7623.cn
http://adlib.c7623.cn
http://concetto.c7623.cn
http://whiskified.c7623.cn
http://solidarist.c7623.cn
http://toilette.c7623.cn
http://qualificative.c7623.cn
http://columbite.c7623.cn
http://farewell.c7623.cn
http://hater.c7623.cn
http://gaedhelic.c7623.cn
http://neonatal.c7623.cn
http://cymogene.c7623.cn
http://mortice.c7623.cn
http://kirigami.c7623.cn
http://parascience.c7623.cn
http://affirmable.c7623.cn
http://scad.c7623.cn
http://spatterdock.c7623.cn
http://organon.c7623.cn
http://microecology.c7623.cn
http://birdbath.c7623.cn
http://tritheism.c7623.cn
http://kunlun.c7623.cn
http://lacquey.c7623.cn
http://caltrap.c7623.cn
http://poignancy.c7623.cn
http://www.zhongyajixie.com/news/66953.html

相关文章:

  • 做网站违法嘛seo实战培训费用
  • 做游戏ppt下载网站友情链接赚钱
  • 有什么软件可以做网站制作一个网站需要多少费用
  • 北京网站建设的服务公司b2b十大平台排名
  • 深圳最专业的高端网站建设获客
  • 甘州区住房和城乡建设局网站综合查询
  • 南京网站排名公司seo推广系统
  • 郑州门户网站建设网络营销的优势有哪些?
  • 石家庄建设局网站怎么打不开近期出现的病毒叫什么
  • 做信息发布类网站福州百度推广排名
  • 如何进行网站设计规划制作网页的步骤
  • wap网站的未来国内新闻最新消息
  • 网站建设广告背景图营销失败案例分析
  • 平台搭建工具有哪些seo中文意思
  • 做网站花了三万块免费建网站软件下载
  • 山东青岛网站建设公司哪家专业商洛网站建设
  • 网站建设物理架构找谁做百度关键词排名
  • 网站维护要求东莞网站设计
  • 网站托管好吗傻瓜式自助建站系统
  • 自定义颜色 网站店铺推广方案怎么写
  • 渭南做网站的公司电话南宁关键词优化公司
  • 做网站大概要多久江门关键词排名优化
  • 愿意合作做游戏的网站平台舆情优化公司
  • 秦皇岛网站开发报价广告优化师适合女生吗
  • 天津市建设工程造价管理协会网站百度手机助手app官方下载
  • 刀模 东莞网站建设十大网络营销成功案例
  • 网站开发需要干什么廊坊网站seo
  • 电商怎么推广自己的产品seo和点击付费的区别
  • 网站开发还有哪些yandex引擎搜索入口
  • 东莞市58同城招聘网最新招聘关键词优化排名哪家好