I spent some time messing with openpyxl and had it doing more or less what I wanted until I introduced some real-world data (only as a test) and discovered it can't handle data validation in the Excel workbook (I think the data validation thing refers to a drop-down list):
UserWarning: Data Validation extension is not supported and will be removed
My code carries on and opens the Excel file but because the validation is removed it breaks it.
Okay, so back to square one, I go away and do some more reading and discover that xlwings can probably handle this. I get xlwings and run the shortest test I can think of:
from pathlib import Path
import xlwings as xw
my_documents = Path('/users/chris/documents')
xw.Book()
wb = xw.Book(my_documents / 'Test.xlsx')
ws = wb.sheets['Sheet1']
That gives a lot of errors for such a small piece of code!
Traceback (most recent call last):
File "/Users/chris/Documents/Python Projects/venv/lib/python3.8/site-packages/aeosa/appscript/reference.py", line 482, in __call__
return self.AS_appdata.target().event(self._code, params, atts, codecs=self.AS_appdata).send(timeout, sendflags)
File "/Users/chris/Documents/Python Projects/venv/lib/python3.8/site-packages/aeosa/aem/aemsend.py", line 92, in send
raise EventError(errornum, errormsg, eventresult)
aem.aemsend.EventError: Command failed: The user has declined permission. (-1743)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/chris/Documents/Python Projects/little_tests.py", line 6, in <module>
xw.Book()
File "/Users/chris/Documents/Python Projects/venv/lib/python3.8/site-packages/xlwings/main.py", line 540, in __init__
app = App()
File "/Users/chris/Documents/Python Projects/venv/lib/python3.8/site-packages/xlwings/main.py", line 210, in __init__
self.impl = xlplatform.App(spec=spec, add_book=add_book)
File "/Users/chris/Documents/Python Projects/venv/lib/python3.8/site-packages/xlwings/_xlmac.py", line 68, in __init__
self.activate() # Makes it behave like on Windows
File "/Users/chris/Documents/Python Projects/venv/lib/python3.8/site-packages/xlwings/_xlmac.py", line 103, in activate
appscript.app('System Events').processes[its.unix_id == self.pid].frontmost.set(True)
File "/Users/chris/Documents/Python Projects/venv/lib/python3.8/site-packages/aeosa/appscript/reference.py", line 518, in __call__
raise CommandError(self, (args, kargs), e, self.AS_appdata) from e
appscript.reference.CommandError: Command failed:
OSERROR: -1743
MESSAGE: The user has declined permission.
COMMAND: app('/System/Library/CoreServices/System Events.app').processes[its.unix_id == 4976].frontmost.set(True)
(venv) chris@Chriss-MacBook-Pro Python Projects %
Unusually for this site, there aren't many references to this here and a wider Google search also doesn't shed any light on it. There seem to be a few people with a similar problem but not so many and nobody I've yet seen seems to have a solution.
Does anybody have any ideas?