我正在尝试使用docx-mailmerge写入Word模板文件,而当我运行程序时,它将返回此错误:
“ PermissionError:[Errno 13]权限被拒绝:'Letter.docx'”
我在Windows 10上使用python 3.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))
答案 0 :(得分:0)
您试图保存或更改已在屏幕中打开的文档。因此,在Windows中,您需要先关闭它或将其保存在其他位置。在尝试处理异常的过程中包装代码。
try:
## put your whole code here##
except PermissionError:
print('opps you need to close the file first sir')