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

使用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}")


http://www.kler.cn/a/597807.html

相关文章:

  • 银河麒麟桌面版包管理器(五)
  • 使用vscode搭建pywebview集成vue项目示例
  • Android Coil3阶梯preload批量Bitmap拼接扁平宽图,Kotlin
  • Liunx系统Microsoft SQL Server数据库还原
  • 设计模式:创建型、结构型、行为型
  • 【Spring】深入理解 Spring 事务管理
  • Cesium 自定义路径导航材质
  • 运算放大器(一)运算放大器的参数、选型与应用
  • 防火墙设置屏蔽漏洞
  • 【leetcode题解】链表
  • mysql5.7及mysql8的一些特性
  • python包和模块
  • 深入理解指针(1)(C语言版)
  • 计算机操作系统(六) 进程控制与进程通信 (附带图谱更好对比理解)
  • 指针,数组 易混题解析(一)
  • 在 .NET 9.0 Web API 中实现 Scalar 接口文档及JWT集成
  • JavaEE-MyBatis概述第一个程序
  • 生活电子常识-deepseek-r1本地化部署+ui界面搭建
  • 练习:自动驾驶
  • xy轴不等比缩放问题——AUTOCAD c#二次开发