denied load_workbook() permission when run through a Service on macOS Catalina
Created originally on Bitbucket by benjami54 (Ben Fockter)
I wrote a short Python (v.3.8.2) script that inserts text into an Excel file.
#!/usr/bin/env python3
from openpyxl import load_workbook
import sys
def insert_to_excel(word):
file_path = '/Users/ben/Desktop/Ben_Test.xlsx'
book = load_workbook(file_path)
worksheet = book.active
worksheet.cell(row=worksheet.max_row + 1, column=2, value=word)
book.save(file_path)
insert_to_excel(sys.argv[1])
I'm calling this .py
file via Run Shell Script
from a Service in Automator.
When I run the python file through either PyCharm or Terminal, it works_._
When I add a Get Specified Text
before the Run Shell Script
in the Service and pass some string as an argument, it works.
However, when I try to invoke the service from any other application I get a PermissionError: [Errno 1] Operation not permitted
:
The action “Run Shell Script” encountered an error: “Traceback (most recent call last):
File "./writer.py", line 15, in <module>
insert_to_excel(sys.argv[1])
File "./writer.py", line 9, in insert_to_excel
book = load_workbook(file_path)
File "/Users/ben/PycharmProjects/Excel_writer/venv/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 312, in load_workbook
reader = ExcelReader(filename, read_only, keep_vba,
File "/Users/ben/PycharmProjects/Excel_writer/venv/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 124, in init
self.archive = _validate_archive(fn)
File "/Users/ben/PycharmProjects/Excel_writer/venv/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 96, in _validate_archive
archive = ZipFile(filename, 'r')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/zipfile.py", line 1251, in init
self.fp = io.open(file, filemode)
PermissionError: [Errno 1] Operation not permitted: '/Users/ben/Desktop/Ben_Test.xlsx'”
Attachments: error