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

使用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 Document

def 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}")
        return
    if not os.path.isdir(docx_folder):
        print(f"Error: The docx folder {docx_folder} does not exist.")
        return
    try:
        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 sys
    try:
        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 enabledelayedexpansion

rem 修改为实际的 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 enabledelayedexpansion

rem 修改为实际的 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 Document

def 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}")
        return

    docx_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}")
        return

    for 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 sys
    try:
        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}")
相关推荐
Eternity_GQM19 分钟前
【Word VBA Zotero 引用宏错误分析与改正指南】【解决[21–23]参考文献格式插入超链接问题】
开发语言·c#·word
来自天蝎座的孙孙1 小时前
洛谷P1595讲解(加强版)+错排讲解
python·算法
张子夜 iiii2 小时前
机器学习算法系列专栏:主成分分析(PCA)降维算法(初学者)
人工智能·python·算法·机器学习
跟橙姐学代码3 小时前
学Python像学做人:从基础语法到人生哲理的成长之路
前端·python
Keying,,,,3 小时前
力扣hot100 | 矩阵 | 73. 矩阵置零、54. 螺旋矩阵、48. 旋转图像、240. 搜索二维矩阵 II
python·算法·leetcode·矩阵
桃源学社(接毕设)4 小时前
基于人工智能和物联网融合跌倒监控系统(LW+源码+讲解+部署)
人工智能·python·单片机·yolov8
yunhuibin4 小时前
pycharm2025导入anaconda创建的各个AI环境
人工智能·python
杨荧4 小时前
基于Python的电影评论数据分析系统 Python+Django+Vue.js
大数据·前端·vue.js·python
python-行者4 小时前
akamai鼠标轨迹
爬虫·python·计算机外设·akamai
R-G-B5 小时前
【P14 3-6 】OpenCV Python——视频加载、摄像头调用、视频基本信息获取(宽、高、帧率、总帧数)
python·opencv·视频加载·摄像头调用·获取视频基本信息·获取视频帧率·获取视频帧数