I'm writing a Python program using PySimpleGUI and docxtpl which writes to a Word template.
I need a way to check if file is currently open by the user, and issue a warning message. Currently if the user has the Word document open when they click "Create Template", the program will crash with Permission Denied error:
self.fp = io.open(file, filemode) ^^^^^^^^^^^^^^^^^^^^^^^ PermissionError: [Errno 13]
# Render the template, save new word document & inform user
print(values)
doc.render(values)
file_name = f"DP-{values['PROPOSAL_NAME']} Q-{values['QUOTE_NO']}_{values['QUOTE_DATE']}.docx"
output_path = Path(values['SAVE_FOLDER']) / file_name
print(output_path)
print(open(output_path))
print(open(output_path).closed)
print(open(output_path)._checkClosed)
if open(output_path):
sg.popup("WARNING: File is open!","Close file before continuing.")
elif open(output_path).closed:
doc.save(output_path)
sg.popup("File saved", f"File has been saved here: {output_path}")
window.close()
See link to another user's question which I've read through but could not find a solution: check if a file is open in Python