# coding=utf-8
import os,csv,docx,codecs
#路径
old_file_path = "./word/old/"# 放了一些docx 文件
new_file_path = "./word/new/"# 生成新文件后的存放地址
fpath = './word/dict/'#字典路径
#字典初始化
replace_dict = {}
def csv_dict(fpath):#csv转换成字典
for fn in os.listdir(fpath):
dict_file = fpath+fn
if fn.split(".")[1]=='csv':
with codecs.open(dict_file,'r',encoding='gbk') as f:
reader = csv.DictReader(f)
for row in reader:
csv_dict = dict(row)
replace_dict.update(csv_dict)
def check_and_change(document, replace_dict, new_file):#docx分为段落里的run和表格里的cell两部分逐个替换
j=0
k=0#敏感词计数
###check敏感词
for para in document.paragraphs:
for i in range(len(para.runs)):
for key, value in replace_dict.items():
j=j+para.runs[i].text.count(key)
for table in document.tables:
for row in table.rows:
for cell in row.cells:
for key, value in replace_dict.items():
k=k+cell.text.count(key)
if j+k>0:#若j+k大于零说明有敏感词
###change敏感词
for para in document.paragraphs:
for i in range(len(para.runs)):
for key, value in replace_dict.items():
if key in para.runs[i].text:
para.runs[i].text = para.runs[i].text.replace(key, value)
for table in document.tables:
for row in table.rows:
for cell in row.cells:
for key, value in replace_dict.items():
cell.text = cell.text.replace(key, value)
document.save(new_file)#保存新文件
print("新文件路径:"+new_file)
return document
def main():
csv_dict(fpath)
for name in os.listdir(old_file_path):
print("源文件名:"+name)
old_file = old_file_path + name
new_file = new_file_path + name
if name.split(".")[1] == 'docx':
document = docx.Document(old_file)
document = check_and_change(document, replace_dict, new_file)
print("^"*30)
if __name__ == '__main__':
main()