使用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}")
相关推荐
灏瀚星空9 分钟前
Python环境依赖管理之终极指南:从隔离原理到深度维护
经验分享·笔记·python·学习方法
C++业余爱好者2 小时前
ModuleNotFoundError: No module named ‘flask‘ 错误
后端·python·flask
钢铁男儿5 小时前
Python 用户账户(让用户拥有自己的数据)
数据库·python·sqlite
FreakStudio6 小时前
一文速通Python并行计算:02 Python多线程编程-threading模块、线程的创建和查询与守护线程
python·嵌入式·多线程·并行计算·电子diy
冷琴19967 小时前
基于python+django的酒店预定网站-酒店管理系统源码+运行步骤+课程学习
python·django·旅游
蹦蹦跳跳真可爱5897 小时前
Python---数据分析(Pandas十一:二维数组DataFrame统计计算二)
python·数据分析·pandas
ice_junjun7 小时前
OpenCV Video 模块使用指南(Python 版)
人工智能·python·opencv
一问三不知_8 小时前
pyqt5报错:qt.qpa.plugin: Could not find the Qt platform plugin “xcb“(已解决)
开发语言·python·qt·ubuntu·conda·bug
赛卡8 小时前
自动驾驶背后的数学:ReLU,Sigmoid, Leaky ReLU, PReLU,Swish等激活函数解析
人工智能·pytorch·python·神经网络·机器学习·数学建模·自动驾驶
小白的高手之路8 小时前
Pytorch中的数据加载
开发语言·人工智能·pytorch·python·深度学习·机器学习