importosimportiofrompython_docx_replaceimportdocx_replace,docx_get_keysfromdocximportDocumentfromrandomimportrandrangestudent_list='''1,张三,2202330301
2,李四,2202330302
3,王五,2202330303
'''review=["思路清晰、语言表达准确,整体表现良好",",准备工作一般,整体表现良好","思路清晰、语言表达一般、回答问题有理论依据,","有个别格式不对的需要修改。"]score=['70','88','81']students=student_list.split("\n")# print(students)students_dict_array=[]forstudentinstudents:student_dict={}student_dict["name"]=student.split(",")[1]student_dict["sid"]=student.split(",")[2]students_dict_array.append(student_dict)print(students_dict_array)# 图片存放的路径path="C:\\BaiduSyncdisk\\大学生信息安全竞赛评分表\\"defalter(file,name,id,num):"""
替换文件中的字符串
:param file:文件名
:param old_str:就字符串
:param new_str:新字符串
:return:
"""doc=Document(file)keys=docx_get_keys(doc)# Let's suppose the Word document has the keys: ${name} and ${phone}print(keys)# ['name', 'phone']# call the replace function with your key value pairsdocx_replace(doc,name=name,id=id,content=review[randrange(len(review))],score=score[num])doc.save(os.path.join(path,"new",file))# 遍历更改文件名num=0forfileinos.listdir(path):alter(os.path.join(path,file),students_dict_array[num]["name"],students_dict_array[num]["sid"],num)os.rename(os.path.join(path,file),os.path.join(path,"选手-"+students_dict_array[num]["sid"][-2:]+students_dict_array[num]["name"]+"-记录表")+".doc")num=num+1
说些什么吧!