问题
I am using the following xlwings
code on MacOS to read a large Excel spreadsheet containing many formulae to be executed:
import xlwings as xl
app = xl.App(visible=False)
book = app.books.open("large.xlsx")
book.save()
app.kill()
Attempting to execute this code leads to a CommandError
: "Apple event timed out", full stack trace:
File "./open_excel_file.py", line 32, in open_excel_file
book = app.books.open("large.xlsx")
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/xlwings/main.py", line 2889, in open
impl = self.impl.open(fullname, update_links, read_only, format, password, write_res_password,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/xlwings/_xlmac.py", line 209, in open
self.app.xl.open_workbook(workbook_file_name=fullname, update_links=update_links, read_only=read_only,
File "/Library/Frameworks/Python.framework/Versions/3.8/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: -1712
MESSAGE: Apple event timed out.
COMMAND: app('/Applications/Microsoft Excel.app', newinstance=(0, 34521322)).open_workbook(workbook_file_name='large.xlsx', update_links=k.do_not_update_links, read_only=None, format=None, password=None, write_reserved_password=None, ignore_read_only_recommended=None, origin=None, delimiter=None, editable=None, notify=None, converter=None, add_to_mru=None)
Is there a way to use xlwings
to open this file, execute all of the formulae and then save it again, without encountering this timeout?
回答1:
xlwings currently goes with the default timeout from the underlying appscript. If this happens during the save event, you should be able to do this as a workaround until this is fixed: workbook.api.save(timeout=3000)
, see: https://github.com/xlwings/xlwings/issues/618
Edit:
To open a workbook works like this:
import xlwings as xw
app = xw.App(visible=False)
book = app.api.open_workbook(workbook_file_name='/full/path/to/large.xlsx', timeout=3000)
I'll try to add native support with the next release.
来源:https://stackoverflow.com/questions/60665968/apple-event-timed-out-using-xlwings-to-open-large-spreadsheet