使用BAT批处理加PYTHON进行WORD批量文字删除

server/2025/3/26 2:40:26/

使用BAT批处理加PYTHON进行WORD批量文字删除,需要删除的文字存放在txt中,编码为UTF-8,文件名为remove_words.txt

安装pip install python-docxpip install chardet

remove_text.py代码

import os
import chardet
from docx import Documentdef remove_text_from_docx(docx_path, text_list):doc = Document(docx_path)for paragraph in doc.paragraphs:for text in text_list:if text in paragraph.text:paragraph.text = paragraph.text.replace(text, '')doc.save(docx_path)def main(txt_path, docx_folder):try:# 检测文件编码with open(txt_path, 'rb') as f:raw_data = f.read()result = chardet.detect(raw_data)encoding = result['encoding']with open(txt_path, 'r', encoding=encoding) as f:text_list = [line.strip() for line in f.readlines()]except Exception as e:print(f"Error reading txt file: {e}")returnif not os.path.isdir(docx_folder):print(f"Error: The docx folder {docx_folder} does not exist.")returntry:for root, dirs, files in os.walk(docx_folder):for file in files:if file.endswith('.docx'):docx_path = os.path.join(root, file)print(f"Processing file: {docx_path}")remove_text_from_docx(docx_path, text_list)except Exception as e:print(f"Error processing docx files: {e}")if __name__ == "__main__":import systry:if len(sys.argv) != 3:print("Usage: python remove_text.py <txt_path> <docx_folder>")else:txt_path = sys.argv[1]docx_folder = sys.argv[2]main(txt_path, docx_folder)except Exception as e:print(f"An unexpected error occurred: {e}")

bat代码

@echo off
chcp 65001 
setlocal enabledelayedexpansionrem 修改为实际的 txt 文件路径
set txt_path=C:\Users\Admin\Desktop\remove_words.txt
rem 修改为实际的 docx 文件所在文件夹路径
set docx_folder=C:\Users\Admin\Desktop\文档
rem python.exe的位置
set python_path=C:\Users\Admin\AppData\Local\Programs\Python\Python313\python.exe%python_path% C:\Users\Admin\Desktop\remove_text.py %txt_path% %docx_folder%pause>nul
endlocal

请使用管理员权限运行BAT

支持通配符版本如下

@echo off
setlocal enabledelayedexpansionrem 修改为实际的 txt 文件路径
set txt_path=路径
rem 使用通配符指定 docx 文件所在的文件夹模式
set docx_folder_pattern=路径*set python_path=路径echo "Python path: %python_path%"
echo "Txt file path: %txt_path%"
echo "Docx folder pattern: %docx_folder_pattern%"%python_path% 路径\remove_text.py %txt_path% "%docx_folder_pattern%"endlocal
import os
import chardet
import glob
from docx import Documentdef remove_text_from_docx(docx_path, text_list):doc = Document(docx_path)for paragraph in doc.paragraphs:for text in text_list:if text in paragraph.text:paragraph.text = paragraph.text.replace(text, '')doc.save(docx_path)def main(txt_path, docx_folder_pattern):try:# 检测文件编码with open(txt_path, 'rb') as f:raw_data = f.read()result = chardet.detect(raw_data)encoding = result['encoding']with open(txt_path, 'r', encoding=encoding) as f:text_list = [line.strip() for line in f.readlines()]except Exception as e:print(f"Error reading txt file: {e}")returndocx_files = glob.glob(os.path.join(docx_folder_pattern, '*.docx'))if not docx_files:print(f"No docx files found matching the pattern: {docx_folder_pattern}")returnfor docx_path in docx_files:try:print(f"Processing file: {docx_path}")remove_text_from_docx(docx_path, text_list)except Exception as e:print(f"Error processing {docx_path}: {e}")if __name__ == "__main__":import systry:if len(sys.argv) != 3:print("Usage: python remove_text.py <txt_path> <docx_folder_pattern>")else:txt_path = sys.argv[1]docx_folder_pattern = sys.argv[2]main(txt_path, docx_folder_pattern)except Exception as e:print(f"An unexpected error occurred: {e}")

http://www.ppmy.cn/server/178518.html

相关文章

Cannon.js 物理引擎入门(Three.js 结合 Cannon.js)

Cannon.js 是一个基于 JavaScript 的 3D 物理引擎&#xff0c;通常与 Three.js 结合使用&#xff0c;来实现刚体碰撞、重力、弹跳、关节约束等物理效果。 1️⃣ 安装 Cannon.js 如果使用 npm&#xff1a; npm install cannon-es 如果用 CDN 方式&#xff0c;可以在 HTML 文件…

Redis安装与配置:从萌新入门到生产环境搭建

各位即将踏入Redis世界的新手司机们&#xff01;今天我们来手把手教你把Redis这辆超跑开上路&#xff0c;从零安装到生产级配置&#xff0c;全程无尿点&#xff01;准备好你的终端&#xff0c;咱们直接开干&#xff01; &#x1f4bb; 一、安装篇&#xff1a;多平台征服指南 1…

【愚公系列】《高效使用DeepSeek》024-儿童教育

🌟【技术大咖愚公搬代码:全栈专家的成长之路,你关注的宝藏博主在这里!】🌟 📣开发者圈持续输出高质量干货的"愚公精神"践行者——全网百万开发者都在追更的顶级技术博主! 👉 江湖人称"愚公搬代码",用七年如一日的精神深耕技术领域,以"…

当了5年牛马,我开始划水了。。。

我现在的这份工作&#xff0c;比上一份要好很多&#xff0c;首先薪资直接涨了一倍&#xff0c;7k到16.5k&#xff0c;13薪&#xff0c;朝九晚六&#xff0c;从不加班&#xff0c;项目也简单&#xff0c;包括我在内测试组一共有6个同事&#xff0c;但是每个人分到的任务真的很少…

数据结构二叉树进阶

1.根据二叉树创建字符串 1.题目 2. 分析原理 要把二叉树元素按照前序顺序取出来&#xff0c;并且以字符串的形式返回&#xff0c;还要添加括号对于左子树和右子树&#xff0c;那么第一步就是向定义一个string类型来接收取出的元素&#xff0c;需要用到to_string函数把整型变成…

基于Python+Django的二手房信息管理系统

项目介绍 PythonDjango二手房信息管理系统(Pycharm Django Vue Mysql) 平台采用B/S结构&#xff0c;后端采用主流的Python语言进行开发&#xff0c;前端采用主流的Vue.js进行开发。 整个平台包括前台和后台两个部分。 - 前台功能包括&#xff1a;首页、二手房信息、公告管理、…

第七节 MATLAB数据类型

默认情况下&#xff0c;MATLAB 存储所有数值变量为双精度浮点值。其他数据类型存储文本&#xff0c;整数或单精度值或单个变量中相关数据的组合。 MATLAB不需要任何类型声明或维度语句。当MATLAB遇到新的变量名称时&#xff0c;它将创建变量并分配适当的内存空间。 如果变量已…

计算机的基本组合和工作原理

计算机的基本组成和工作原理可以概括为以下几个核心部分&#xff1a; 一、计算机的基本组成&#xff08;冯诺依曼体系结构&#xff09; 现代计算机基于冯诺依曼体系结构&#xff0c;主要由以下五大部件组成&#xff1a; 控制器&#xff08;Control Unit, CU&#xff09; 功能&…