# coding=utf-8
import os
from docx import Document
old_file_path='C:\\Users\\lgs\\Desktop\\docx\\'
new_file_path='C:\\Users\\lgs\\Desktop\\new_docx\\'
replace_dict={
"2015年版一部":"2020年版一部",
"2012年版":"2020年版",
"SDFLX/A/":"SDFLX/B/"
}
def check_and_change(document,replace_dict):
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:
print(key+"-->"+value)
para.runs[i].text=para.runs[i].text.replace(key,value)
return document
def main():
for name in os.listdir(old_file_path):
print(name)
old_file = old_file_path + name
new_file = new_file_path + name[:18]+"(B00版).docx"
if old_file.split(".")[1] == 'docx':
document = Document(old_file)
document = check_and_change(document, replace_dict)
document.save(new_file)
print("^"*30)
if __name__ == '__main__':
main()