使用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}")
相关推荐
Blossom.1188 分钟前
用一颗MCU跑通7B大模型:RISC-V+SRAM极致量化实战
人工智能·python·单片机·嵌入式硬件·opencv·机器学习·risc-v
工业互联网专业25 分钟前
基于大数据的学习资源推送系统的设计与实现 _django
vue.js·python·django·毕业设计·源码·课程设计·学习资源推送系统
木子杳衫3 小时前
【软件开发】管理类系统
python·web开发
程序员小远6 小时前
银行测试:第三方支付平台业务流,功能/性能/安全测试方法
自动化测试·软件测试·python·功能测试·测试工具·性能测试·安全性测试
猫头虎9 小时前
如何查看局域网内IP冲突问题?如何查看局域网IP环绕问题?arp -a命令如何使用?
网络·python·网络协议·tcp/ip·开源·pandas·pip
沿着路走到底9 小时前
python 基础
开发语言·python
烛阴10 小时前
武装你的Python“工具箱”:盘点10个你必须熟练掌握的核心方法
前端·python
杨枝甘露小码11 小时前
Python学习之基础篇
开发语言·python
我是华为OD~HR~栗栗呀11 小时前
23届考研-Java面经(华为OD)
java·c++·python·华为od·华为·面试
小蕾Java12 小时前
PyCharm 软件使用各种问题 ,解决教程
ide·python·pycharm