编程语言
首页 > 编程语言> > Python_报错:PermissionError: [Errno 13] Permission denied: 'XXXXX.xlsx'

Python_报错:PermissionError: [Errno 13] Permission denied: 'XXXXX.xlsx'

作者:互联网

Python_报错:

D:\Python36\python.exe D:/test_excel/excel_001.py
Traceback (most recent call last):
File "D:/test_excel/excel_001.py", line 14, in <module>
wb.save("e:\\sample.xlsx")
File "D:\Python36\lib\site-packages\openpyxl\workbook\workbook.py", line 367, in save
save_workbook(self, filename)
File "D:\Python36\lib\site-packages\openpyxl\writer\excel.py", line 282, in save_workbook
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
File "D:\Python36\lib\zipfile.py", line 1090, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'e:\\sample.xlsx'

源码:

import locale
from openpyxl import Workbook
import datetime
import time
import locale
wb = Workbook()
ws = wb.active
ws['A2']= datetime.datetime.now()#写入一个当前时间
#写入一个自定义的时间格式
locale.setlocale(locale.LC_CTYPE,'chinese')
ws['A3'] = time.strftime("%Y年%m月%d日 %H时%M分%S秒",time.localtime())

# Save the file
wb.save("e:\\sample.xlsx")

原因:excel文件是打开的,资源被占用,在进行操作时,关闭写入的文件即可

运行结果:

 

标签:xlsx,13,wb,locale,excel,报错,Python36,import,save
来源: https://www.cnblogs.com/rychh/p/11421317.html