0

I am trying to write a script that live edits an open excel file, but when I try to run the script that uses openpyxl and reads from a cell then writes data back to that cell with an edit, it gives this error PermissionError: [Errno 13] Permission denied: 'GameExcel.xlsx' is there a way around this using another module, or is there a secret I am missing

Edit here's the code, also this is just me learning before I integrate it into the full code.

import openpyxl
from openpyxl import load_workbook
from openpyxl import workbook
from openpyxl.utils import get_column_letter
import os
import tkinter as tk
from tkinter import messagebox as tkMsgBox
import time

os.chdir("D:\Scripts\Python\Testing Scripts\My Excel Game")
wb = load_workbook("GameExcel.xlsx")
names = wb.sheetnames
sheet = wb['GameEnviroment']

#userInput = (input("what would you like it to say?"))
#print(userInput)
C3Val = sheet['C4'].value

sheet.cell(row=3, column=4).value = (C3Val + ' 4')
wb.save('GameExcel.xlsx')
print(C3Val + ' 3')
#sheet['A1']=userInput
Nickiel
  • 99
  • 1
  • 3
  • 13

2 Answers2

2

This is due to the operating system limitation (ie Windows). It has nothing to do with openpyxl, Python or even Excel. POSIX based OS do not have such a limitation.

The answer to this question ("How to bypass permissions to write to an open file") is simply "You can't".

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • is there a workaround? or do I need two identical excels that every time one updates the open one closes and updates while the updated one opens? – Nickiel Jul 15 '18 at 15:45
  • @Nickiel The only workaround would be to use another mechanism that allows concurrency, such as a database. Or a different operating system – DeepSpace Jul 15 '18 at 15:46
  • database? is there a way to select and write text? I have been use a scripting language called autohotkey, so that is kinda what I am thinking in terms of how code structure is, haven't really gotten the hang of it. – Nickiel Jul 15 '18 at 15:49
  • @Nickiel Yes, that's kinda the purpose of databases – DeepSpace Jul 15 '18 at 15:53
  • @Nickiel Without more context it's hard to give you concrete advice. Maybe something like [xlwings](https://www.xlwings.org/) does help you more? I think with xlwings it's possible to directly edit content in excel with python, instead of editing a file that excel than would need to read. – syntonym Jul 15 '18 at 15:54
  • ok i'll look into those... can they write to an open excel file? or do they do something else? – Nickiel Jul 15 '18 at 15:54
  • I put the script in the question – Nickiel Jul 15 '18 at 15:56
  • Voted to close. @Nickiel please don't be discouraged by SO and this isn't personal, the question doesn't fit SO's format as it is a "help me debug my code" problem. That said, off the top of my head, perhaps a plugin running inside the spreadsheet software would work better, otherwise most applications lock the file to prevent something like this happening to avoid concurrent users corrupting the file. – David Jul 15 '18 at 16:02
  • ok, very well, I posted my code because @syntonym said he needed more context – Nickiel Jul 15 '18 at 16:04
  • would i have to write the plugin? – Nickiel Jul 15 '18 at 16:08
  • With more context I meant more what you actually want to archieve. DeepSpace answers your question that there is no way on windows to write to on already opened file. I would suggest looking into xlwings because there you can integrate python code with excel. If you have another concrete question I would suggest asking a new one here on SO. – syntonym Jul 15 '18 at 17:45
1

The option that I went with, and this only works with excel open, is xlwings

Nickiel
  • 99
  • 1
  • 3
  • 13