使用python不改变格式的情况下批量替换word里面的内容

需要使用如$name,${id}这样的模板在这里插入图片描述

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
'''


review=["思路清晰、语言表达准确,整体表现良好",",准备工作一般,整体表现良好","思路清晰、语言表达一般、回答问题有理论依据,","有个别格式不对的需要修改。"]
score=['70', '88', '81']

students=student_list.split("\n")

# print(students)
students_dict_array=[]

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\\大学生信息安全竞赛评分表\\"
def alter(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

参考:https://stackoverflow.com/a/72639838/10096812

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值