我有一个输出大熊猫数据框的程序。该程序在监视应用程序中使用,因此最终用户必须在给定的excel工作簿(例如“ Output.xlsx”)中连续查看该程序的输出。 我想使程序尽可能快,所以我将数据帧写入csv文件“ Speed.csv”,然后将csv文件转换为“ Output.xlsx”。
from openpyxl import Workbook
import csv
def speed_writing(df):
df.to_csv(r'Speed.csv')
def csv_to_excel():
wb = Workbook()
ws = wb.active
with open('Speed.csv', 'r') as f:
for row in csv.reader(f):
ws.append(row)
wb.save('Output.xlsx')
speed_writing(df)
csv_to_excel()
现在,仅当关闭“ Output.xlsx”时,此方法才有效。如果我打开文件,请运行代码,得到以下回溯:
Traceback (most recent call last):
File "trade_export.py", line 140, in <module>
csv_to_excel()
File "trade_export.py", line 130, in csv_to_excel
wb.save('Output.xlsx')
File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\openpyxl\workbook\workbook.py", line 391, in save
save_workbook(self, filename)
File "C:\Users\\AppData\Local\Programs\Python\Python36\lib\site-packages\openpyxl\writer\excel.py", line 282, in save_workbook
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
File "C:\Users\AppData\Local\Programs\Python\Python36\lib\zipfile.py", line 1113, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'Output.xlsx'
这是由于本地设置还是因为我无法使用openpyxl保存打开的文件? 任何提示,欢迎解决方案!