import os import io from python_docx_replace import docx_replace,docx_get_keys from docx import Document from random import randrange student_list='''1,张三,2202330301 2,李四,2202330302 3,王五,2202330303 '''
for student in students: 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 pairs docx_replace(doc, name=name,id=id,content=review[randrange(len(review))],score=score[num]) doc.save(os.path.join(path,"new",file))
# 遍历更改文件名 num = 0 for file in os.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
说些什么吧!