我试图使用docx mailmerge写入word模板文件,当我运行程序时,它返回以下错误:
“权限错误:[Errno 13]权限被拒绝:'Letter.docx'”
我在Windows10上使用Python3.81。请帮忙。下面是我的代码中处理写入模板的部分。
document = MailMerge(template)
print(document.get_merge_fields())
print(GlobalIntro)
document.merge(Letter=GlobalIntro)
if WaitAndWatchVariable == 1:
print(WaitAndWatchPara)
document.merge(WaitAndWatch=str(WaitAndWatchPara))
if RestorativeTreatmentVariable == 1:
print(RestorativeTreatmentPara)
document.merge(RestorativeTreatment=str(RestorativeTreatmentPara))
if CrownTreatmentVariable == 1:
print(CrownTreatmentPara)
document.merge(CrownTreatment=str(CrownTreatmentPara))
if RootCanalTreatmentVariable == 1:
print(RootCanalTreatmentPara)
document.merge(RootCanal=str(RootCanalTreatmentPara))
if WisdomTeethVariable == 1:
print(WisdomTeethPara)
document.merge(WisdomTeeth=str(WisdomTeethPara))
if WaitAndWatchOnWisdomTeethVariable == 1:
print(WaitAndWatchOnWisdomTeethPara)
document.merge(WaitAndWatchOnWisdomTeeth=str(WaitAndWatchOnWisdomTeethPara))
if ExtractingTreatmentVariable == 1:
print(ExtractionTreatmentPara)
document.merge(ExtractionTreatment=str(ExtractionTreatmentPara))
您试图保存或更改已在屏幕中打开的文档。所以在窗口中,你需要先关闭它或者把它保存在其他地方。包装你的代码尝试和处理异常。
try:
## put your whole code here##
except PermissionError:
print('opps you need to close the file first sir')