python word文档文字批量替换与删除
当遇到需要进行大量文档的内容进行替换或者删除(替换为空)的操作时,可以借鉴思路。
import re
import os
#需要批量替换内容文件所处的文件夹地址
filepath = r'D:\Python-study\word-test\test'
#该文件夹下的所有文件名列表
fileList = os.listdir(filepath)
print(fileList)
#保存文件的路径
savepath = r'D:\Python-study\word-test\delete-done'
import docx
#需要在docx格式下的word才可以处理
for docxname in fileList:
print(docxname)
docxpathname = filepath + '\\' + docxname
#print(docxpathname)
document = docx.Document(docxpathname)
#print(document)
###开始替换文本内容###
#删除[
for paragraph in document.paragraphs:
for run in paragraph.runs:
if "[" in run.text:
print(run.text)
run.text=run.text.replace('[','')
#删除0
for paragraph in document.paragraphs:
for run in paragraph.runs:
if "0" in run.text:
print(run.text)
run.text=run.text.replace('0','')
#1替换成x
for paragraph in document.paragraphs:
for run in paragraph.runs:
if "1" in run.text:
print(run.text)
run.text=run.text.replace('1','x')
#保存修改后的文件
savepathname = savepath + '\\' + docxname
document.save(savepathname)
print(docxname + " 已完成")
print("全部处理完成")