0

I'm working on a project which I am going to write values to a exceldocument. I can write values to excel but I want to write the values to excel with the document open. If i have the doc open when trying to write to it i get an error message.

Here is my code I'm currently using.

#Write to excel
from openpyxl import Workbook

import datetime

wb = Workbook()

ws = wb.active

ws['C3'] = 1337

ws['A1'] = datetime.datetime.now()

ws['B5'] = CIRCLES
ws['B4'] = "Red puck"
wb.save("sample.xlsx")

And the error message when i have the doc. opened when trying to write to it.

Traceback (most recent call last):
  File "C:\Users\RU21\Desktop\Röda puckar\Förbindelse med RR.py", line 302, in <module>
    wb.save("sample.xlsx")
  File "C:\Python27\lib\site-packages\openpyxl\workbook\workbook.py", line 280, in save
    save_workbook(self, filename)
  File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 211, in save_workbook
    writer.save(filename)
  File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 193, in save
    archive = ZipFile(filename, 'w', ZIP_DEFLATED)
  File "C:\Python27\lib\zipfile.py", line 756, in __init__
    self.fp = open(file, modeDict[mode])
IOError: [Errno 13] Permission denied: 'sample.xlsx'
  • check file permission – Hasan Ramezani Oct 01 '14 at 12:15
  • 1
    you can not write to a file when you have opened it in read/write mode. The latest excel has a sharing option, for which simultaneaous writing is possible (more of a merging functionality). You would have to set it for the excel file and pray openpyxl supports it. – nire Oct 01 '14 at 12:19
  • Thanks, I will have to try that then. – Jonny Andreasson Oct 01 '14 at 12:53
  • 1
    @nire that's irrelevant here. This is an error message from the operating system that you can't save "sample.xlsx" in a particular folder. You may have to supply the whole path where you want to save the file, ie. r"C:\Users\RU21\Desktop\Röda puckar\sample.xlsx". The preceding r is important. – Charlie Clark Oct 01 '14 at 12:56

0 Answers0