问题遇到的现象和发生背景
先读取指定excel表格数据,python 批量产生word模板,在每次循环时对document.merge()函数里的实时参数已更新(debug验证过),但是产生的模板内容都与第一次产生的模板内容相同,数据不会更新
问题相关代码,请勿粘贴截图
from openpyxl import Workbook, load_workbook
from mailmerge import MailMerge
template = '模板.docx'
document = MailMerge(template)
加载excel文件
wb = load_workbook("123.xlsx",data_only=True)
获取所在的工作表
sheet = wb.active
flag=0
countRow=0
lastRow=(0,0)
for row in sheet.iter_rows(min_row=2, max_row=38,min_col=1, max_col=8):
for i in row:
if i.value==None:
flag=1
continue
if (flag==1 and save==0):
name=lastRow[4].value
num=lastRow[3].value
clas=lastRow[0].value
mayor=str(clas)[:-3]
xuefen=row[7].value
if int(xuefen)>=12:
print(name,num,mayor,clas,xuefen)
document.merge(name=name,
num=str(num),
mayor=mayor,
clas=clas,
xuefen=str(xuefen))
document.write('新建文件夹{:}score.docx'.format(name))
save=1
print(i.value,end=', ')
flag = 0
print()
lastRow = row
countRow+=1
运行结果及报错内容
在指定文件夹生成了不同名字的word文档,但是里面的内容相同且均为读取excel数据时第一次循环的内容
我的解答思路和尝试过的方法
我想要达到的结果
生成的模板的内容与excel能对应