I have a program outputing a large pandas dataframe. The program is used in a monitoring application and so the end user must continuously see the output of the program in a given excel workbook ex "Output.xlsx". I want to make the program as fast as possible, so I write the dataframe to a csv file "Speed.csv" and then convert the csv file to "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()
Now, this works well only when "Output.xlsx" is closed. If I open the file, run the code I get the following traceback:
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'
Is this because of local settings or because I cannot save opened files with openpyxl? Any hints, solutions are welcome!