使用BAT批处理加PYTHON进行WORD批量文字删除,需要删除的文字存放在txt中,编码为UTF-8,文件名为remove_words.txt
安装pip install python-docx
和pip 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}")